Selenium can't find elements - python

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

Related

Why I am having an error on send_keys using selenium python

I'm trying to make a script that logs me into my account automatically and I'm stuck at send_keys.
I have the latest version of selenium, I'm using python 2.7.18.
here is my script:
from selenium import webdriver
from time import sleep
username = 'username'
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
sleep(4)
email = '//*[#id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input'
login = driver.find_element_by_xpath(email).send_keys(username)
The error is:
Traceback (most recent call last):
File "C:\Users\Jameel\Desktop\selenium1.py", line 14, in <module>
login = driver.find_element_by_xpath(email).send_keys(username)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 321, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) WebDriverException: Message: unknown error: call function result
missing 'value' (Session info: chrome=83.0.4103.61) (Driver info:
chromedriver=2.31.488763
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT
10.0.17763 x86_64)
I know the script isn't complete.
Generally different sites follow different design approaches, which doesn't allow normal script flow & needs additional steps to be added.
In you case, the default CSS style blocks your input. So, first click on that element( Which makes it as focused) then send your keys.
Element Attribute differences below,
Before click action
After click action

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

Send Keys to a text field using Selenium python

I have to send the string to the textfield in the webpage .
I tried to access it by xpath but the xpath keeps on changing everytime I open the webpage newly. So I decided to access it using class name or tag name. But I am getting an error that the keys can not be passed to the access field.
HTML of the textfiled in the webpage:
<div class = "SearchBox">
<input aria-label = 'xyz' placeholder = 'abc'>
</div>
I tried with these lines of code but none of it is working:
text = driver.find_element_by_xpath('//div[contains(#class,'SearchBox')] and input[contains(#aria-label,'xyz')]')
text = driver.find_element_by_class_name('SearchBox')
text = driver.find_element_by_xpath("//div[#class='SearchBox']/input")
text = driver.find_element_by_xpath("//div[#class='SearchBox']/input[contains(#aria-label,'Combobox expanded. Use arrow keys to select available options or type to search.') and #dojoattachpoint='_searchInput']")
What am I doing wrong? Kindly help.
Error Log:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
l.send_keys("abcdef")
File "C:\Users\Pavan-Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\Pavan-Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\Pavan-Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\Users\Pavan-Kumar\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=65.0.3325.146)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
HTML Code Pic:
The xpath that you jave written contains identifiers of both the div tag and the input tag. How will Selenium understand that you are trying to access the input field!!!!
If you are trying to use the div tag as an anchor to reach the input tag, your xpath should look something like this...
//div[#class='SearchBox']/input
The below xpath should help you as per the html structure image you shared.
//div[#class='SearchBox']/input[contains(#aria-label,'expanded') and #dojoattachpoint='_searchInput']

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

Unable to select an element from a dropdown list with selenium webdriver

I need to select an element from a dropdown list using selenium webdriver in Python. For that I have checked helpful posts such as Selecting a value from a drop-down option using selenium python and
https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver?lq=1.
The element i am talking about is shown in the next block:
<div id="dayTab" style="height:20px" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
<select class="input-small input-thin">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
</div>
I tried Select():
yearselect = Select(browser.find_element_by_css_selector("select.input-small.input-thin"))
yearselect.select_by_value("2010")
Although it locates the element (which it does), i then get the following error which occurs for the second line:
Traceback (most recent call last):
File "C:\Users\elek2\workspace\webdriving\src\gotonch.py", line 119, in <module>
yearselect.select_by_value("2010")
File "C:\Python34\lib\site-packages\selenium\webdriver\support\select.py", line 79, in select_by_value
self._setSelected(opt)
File "C:\Python34\lib\site-packages\selenium\webdriver\support\select.py", line 195, in _setSelected
option.click()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 453, in _execute
return self._parent.execute(command, params)
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 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=45.0.2454.101)
(Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64)
I am not sure why this happens but i have also tried to use Click() instead in order to "open" the drop down list
yearselect =browser.find_element_by_css_selector("select.input-small.input-thin").click()
yearselect.select_by_value("2010")
and that the elements are visible but then I get this:
Traceback (most recent call last):
File "C:\Users\elek2\workspace\webdriving\src\gotonch.py", line 118, in <module>
yearselect = browser.find_element_by_css_selector("select.input-small.input-thin").click()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 453, in _execute
return self._parent.execute(command, params)
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 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=45.0.2454.101)
(Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64)
Why is element still not visible if I am able to locate the drop-down list and select it?
EDIT:
After LINGS comment I realised that there is not only one element with the css name that I 've used.
I am after the above block but there is another block referenced before that one where instead of div id="dayTab"... is div id="monthTab"... which obviously is invisible. How can I refer to the tab that I want, there's no ID.
It was quite simple after all, I replaced the initial:
yearselect = Select(browser.find_element_by_css_selector("select.input-small.input-thin"))
yearselect.select_by_value("2010")
with this:
yearselect = Select(browser.find_element_by_css_selector("#dayTab > select.input-small.input-thin"))
yearselect.select_by_value("2010")
It is only a matter of finding the correct CSS (or XPath). Chrome add-ons such as XPath Helper may help in this. Other tips on CSS selectors you may find in here. Glad in case I've helped other users avoid such annoying mistakes.

Categories

Resources