how to locate an element embedded in iframe for Python Selenium - python

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()

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 selenium cant find iframe xpath

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']"))

Selenium can't find elements

I am trying to login to a site through Selenium. All the sites on which I try to log in, I could do this less. After putting this part of the code shows me a Python 2.7 error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 350, in send_keys
'value': keys_to_typing(value)})
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 499, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.12.6 x86_64)
Code:
# coding=utf-8
from selenium import webdriver
driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')
driver.get("https://car.com")
user= driver.find_element_by_xpath('//*[#id="onevideo_login_username"]')
user.send_keys("email")
Inspect:
<input type="text" id="onevideo_login_username" name="adaptv_email" data-i18n-placeholder="login.username" ng-model="models.login.email" class="ng-pristine ng-valid" placeholder="Username">
As I said before, I can login in all web sites I was trying, but this one is a headache!
Is the url correct?
If you have id,then its better to use,
Try:
user=driver.find_element_by_id('onevideo_login_username');
Please check the URL, I guess your URL is not correct. First of all I did not find any element like this in URL, but as per html I suggest you locator to locate element
<input type="text" id="onevideo_login_username" name="adaptv_email" data-i18n-placeholder="login.username" ng-model="models.login.email" class="ng-pristine ng-valid" placeholder="Username">
Use ID
user=driver.find_element_by_id('onevideo_login_username');
Use Name
user=driver.find_element_by_name('adaptv_email');
Use xpath
user= driver.find_element_by_xpath('//*[#name="adaptv_email"]')

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')

Categories

Resources