python selenium cant find iframe xpath - python

I am having an issue with Selenium and not being able to switch to a frame. I have tried using the xpath of iframe but it replies that the element was not found. This was the code I was using:
driver.switch_to_frame(driver.find_element_by_xpath('//[#id="VF"]'))
I have also tried using a css-selector but it gives me the same result. I suspect that the div element that holds the iframe that I want to select is causing this issue but I'm not sure. Here's what that div element looks like:
<div id="dee_0" class="ar BAResults" fframe='<iframe title="SRC" src="javascript:"<HTML></HTML>"" onload="DVFol()"></iframe>
Any help would be greatly appreciated.
edit
this is the iframe I'm trying to target
<iframe name="VF" title="SRC" id="VF" src='javascript:"<HTML></HTML>"' frameborder="0" scrolling="auto" style="left: 0px; top: 0px; width: 830px; height: 490px; background-color: transparent;" allowtransparency="true" onload="F(0,302899400).ol()">
stack trace
Traceback (most recent call last):
File "C:/Users/kthao.ISC/PycharmProjects/AutoComplete/test2.py", line 32, in
<module>
chrome.switch_to_frame(chrome.find_element_by_xpath('//*
[#id="VF"]'))
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 368, in
find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 237, in
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath","selector":"//*
[#id="VF"]"}

The method to switch to an iframe is switch_to.frame() and the <iframe> tag contains the attribute title as SRC. So you can use the following line of code :
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[#title='SRC']"))

Related

Stuck getting selenium code to click my link

I am trying to click a button but I am having an error when finding the element. In general I am trying to refresh a page until the link to join the waiting list goes live. When I run the script the webpage pulls up but does not refresh or click the join waitlist button when it is live.
I've tried finding element by class, link text, xpath, each time I get an error saying the element wasn't found.
code
while not joinList:
try:
addToCartBtn = addButton = driver.find_element_by_class_name("disabled")
print("Button isnt ready yet.")
time.sleep(2)
driver.refresh()
except:
addToCartBtn = addButton = driver.find_element_by_class_name("signup-btn")
print("Join")
addToCartBtn.click()
joinList = True
Where the element for the disabled join waitlist button is:
button _ngcontent-yur-c149="" disabled="" class="signup-btn ng-tns-c149-6 ng-star-inserted" style="background-color: rgb(0, 59, 153);"> Waitlist is currently closed </button
and the element for the enabled waitlist is:
button _ngcontent-yur-c149="" class="signup-btn ng-tns-c149-4 ng-star-inserted" tabindex="0" style="background-color: rgb(0, 59, 153);"> Join waitlist </button
The error I get is:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\Pycharm\MCattempt2\Main test folder\Test 1.py", line 20, in <module>
addToCartBtn = addButton = driver.find_element_by_class_name("signup-btn")
File "C:\Python\Pycharm\MCattempt2\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Python\Pycharm\MCattempt2\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python\Pycharm\MCattempt2\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python\Pycharm\MCattempt2\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".signup-btn"}
(Session info: chrome=88.0.4324.190)
Any advice on what I am doing wrong?

Get a Text from a Iframe with selenium and python

I need to get text from emails and save for send in webwhatsapp, but the text is in a iframe and returns a error when I try to get him
Thats the html code:
<div id="mailContent" class="mail-content" style="height: 724px;">
<div id="phishingLinkScanContent"></div>
<iframe id="bodyMailViewer"
class="body-mail-viewer" style="height: 719px;"
cd_frame_id_="18a0312a828e12092646c9a6b1ca58c4">
#document
<html><head></head>
<body>MetaTrader email test -> cilabassa#terra.com.br<br><div id="ozoneAttachments"></div></body>
</html>
</iframe>
</div>
Thats my code:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
texto = driver.find_element_by_id('bodyMailViewer').text
Thats the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="bodyMailViewer"]"}
(Session info: chrome=75.0.3770.142)
First switch to the iframe and then locate the body tag under iframe and finally get the text from body tag. Try the below code.
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
textInMail = driver.find_element_by_tag_name('body').text

Python uploading image/file with selenium webdriver

The folowing is a snip of some crowdfire.com HTML code, where im trying to upload a file in the input class =...
<div id="ember1089" class="ember-view">
<div id="ember1090" class="action__addImage pointer ember-view tooltipstered">
<div data-ember-action="1091">
<img class="iconImg iconImg--camera" src="/publish/images/icon-camera-b432ac4c5b369d4616baf097b951d9b4.png"/>
<span>Add an image</span>
</div>
<input class="js-file-input action__fileInput" type="file" data-ember-action="1092"/>
</div>
Now below is what i have so far. I have created driver instance, the code before logs in the website without a problem and locates all other xpaths
image = driver.find_element_by_xpath('.//*[#id=\'ember1089\']/input')
time.sleep(2)
print 'found element'
image.send_keys('C:\Users\Brian\Desktop\Empire_fort\Bots\GetPICtures\Empire.jpg)
print 'uploading'
time.sleep(5)
the following is the error i get, and i really don't understand what is the problem,On the website there is an "add an image" link. if clicked it brings up window's explorer from where the user can select a file and upload it.
waiting
about toupload
found element
Traceback (most recent call last):
File "C:\Users\Brian\Desktop\Empire_fort\Bots\GetPICtures\nowupload.py", line 55, in <module>
image.send_keys(r'C:\Users\Brian\Desktop\Empire_fort\Bots\GetPICtures\Empire.jpg')
File "C:\Python2.7.11\lib\site-packages\selenium\webdriver\remote\webelement.py", line 321, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "C:\Python2.7.11\lib\site-packages\selenium\webdriver\remote\webelement.py", line 456, in _execute
return self._parent.execute(command, params)
File "C:\Python2.7.11\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python2.7.11\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///c:/users/brian/appdata/local/temp/tmpxjigan/extensions/fxdriver#googlecode.com/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/brian/appdata/local/temp/tmpxjigan/extensions/fxdriver#googlecode.com/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/brian/appdata/local/temp/tmpxjigan/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/brian/appdata/local/temp/tmpxjigan/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/brian/appdata/local/temp/tmpxjigan/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
Thanks!
Ps Update, It works with with a webdriver.Chrome() instance but not with Firefox or phantomjs
I suspect you need to make the input visible first:
image = driver.find_element_by_css_selector('#ember1089 input')
driver.execute_script("arguments[0].style.display = 'block';", image);
image.send_keys('C:\Users\Brian\Desktop\Empire_fort\Bots\GetPICtures\Empire.jpg')

how to locate an element embedded in iframe for Python Selenium

I am having problem locating a single report element embedded in iframe. There many reports contains in table where iframe is located. Also using the firefox firePath it is given me a very link and am not sure how to remove the long strings. Edited the code:
browser.switch_to_frame(browser.find_element_by_tag_name("iframe"))
# set the context on the child frame
browser.switch_to_frame(browser.find_element_by_id("__gwt_historyFrame"))
# set the context on the next child frame
wait = WebDriverWait(self.browser, 10)
browser.switch_to_frame(wait.until(EC.presence_of_element_located(By.ID,"com.demandreports.wb.Workbench")))
#click the link
wait.until(EC.presence_of_element_located(By.LINK_TEXT,"Invoice Aging Report")).click()
# switch back to the main document context
browser.switch_to_default_content()
This is error:
Traceback (most recent call last):
File "C:\Python34\zuoraDataexport6.py", line 118, in <module>
scraper.scrape()
File "C:\Python34\zuoraDataexport6.py", line 57, in scrape
browser.switch_to_frame(browser.find_element_by_id("com.demandreports.wb.Workbench"))
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 234, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
{'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"com.demandreports.wb.Workbench"}
This is what i want to locate:
Invoice Aging Report
This is the html:
<document>
<html style="overflow: hidden;">
<head>
<body style="margin: 0px;">
<iframe id="__gwt_historyFrame" style="position:absolute;width:0;height:0;border:0" tabindex="-1" src="javascript:''"/>
<iframe id="com.demandreports.wb.Workbench" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"/>
<table class="Workbench-Page" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<tr>
<tr>
</tbody>
</table>
</body>
</html>
</document>
The context is not set on the frame where your element is. You need to switch to each frame on the tree line:
# set the context on the child frame
browser.switch_to_frame(browser.find_element_by_id("__gwt_historyFrame"))
# set the context on the next child frame
browser.switch_to_frame(browser.find_element_by_id("com.demandreports.wb.Workbench"))
#click the link
wait.until(EC.presence_of_element_located(By.LINK_TEXT,"Invoice Aging Report")).click()
# switch back to the main document context
browser.switch_to_default_content()

How to get rid of specific exceptions in selenium?

I have to use the selenium for my project.when i am doing using selenium.i am giving the at each step like this
time.sleep(6)
i am giving the sufficient interval for loading particular page, but still I am facing the below error at somepoint.
Mostly i am getting the exceptions like
Regular Exceptions:
Exception: Message: u'Unable to locate element: {"method":"xpath","selector":"//textarea[#style=\\"overflow-y: hidden; height: 50px; width: 300px;\\"]"}' ; Stacktrace: Method WebDriverError threw an error in file:///tmp/tmpBxGp2P/extensions/fxdriver#googlecode.com/resource/modules/atoms.js
Traceback (most recent call last):
File "crawl_optimize.py", line 206, in main
word_query = browser.find_element_by_xpath('//textarea[#style="overflow-y: hidden; height: 50px; width: 300px;"]')
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 210, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 643, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py", l ine 147, in check_response
raise exception_class(message, screen, stacktrace)
I am also getting No Such Implementation Error because of WebDriverWait().Until():
NoSuchElementException: Message: u'Unable to locate element: { "method":"xpath","selector":"//textarea[#style=\\"overflow-y: hidden; height: 50px; width: 300px;\\"]"}' ; Stacktrace: Method WebDriverError threw an error in file:///tmp/tmpBxGp2P/extensions/fxdriver#googlecode.com/resource/modules/atoms.js
keyword : balance transfer ad pos : 2
Exception: Message: None
Traceback (most recent call last):
File "crawl_optimize.py", line 228, in main
start_cpc,end_cpc = set_initial_cpc(initial_max_cpc,ad_position)
File "crawl_optimize.py", line 95, in set_initial_cpc
ep = crawl_position(cost)
File "crawl_optimize.py", line 82, in crawl_position
WebDriverWait(browser, 10).until(lambda driver : driver.find_element_by_link_text(word.lower()))
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/support/wait.py", line 55, in until
raise TimeoutException()
TimeoutException: Message: None
What is the way to get rid of such exceptions?
Could be 2 things.
Your element hasn't finished loading within the allocated 6 seconds
Your locator is malformed. This looks suspect ...
{ "method":"xpath","selector":"//textarea[#style=\"overflow-y: hidden; height: 50px; width: 300px;\"]"}'
Can you check that the locator returns what you expect using FireFinder or similar?
If the locator is ok, you could try using WebDriverWait & ExpectedCondition to confirm the page has finished loading rather than gambling that you page will always load in 6s

Categories

Resources