This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 3 years ago.
I have a very simple code where I am looking for a class and when that class doesn't exist I want to catch that and update a status in my CSV. I am implementing this like:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
invalid = browser.find_element_by_class_name('_3lLzD')
try:
Update status
except NoSuchElementException as e:
Update Status
It still throws me the error:
Traceback (most recent call last):
File "C:/Users/Krenovate/Desktop/automate/automate/automate.py", line 35, in <module>
invalid = browser.find_element_by_class_name('_3lLzD')
File "C:\Users\Krenovate\PycharmProjects\mxrecord\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:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Krenovate\PycharmProjects\mxrecord\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: Unable to locate element: ._3lLzD
What am I doing wrong
Per the documentation, the locator function is designed to throw an exception if it is not found. Therefore, the exception that you get is well within the expected behavior.
If you are unsure of the presence of the element, the easiest way is to place it within a try block
try:
invalid = browser.find_element_by_class_name('_3lLzD')
Update status
except NoSuchElementException as e:
Update Status
or use explicit waits with a timeout.
Related
I am following a tutorial of a video on youtube about selenium and I got to this point
This is my code and it works fine until it gives you this massive error of:
Traceback (most recent call last):
File "D:\pythonProject\webscraping.py", line 17, in <module>
search.send_keys(Keys.RETURN)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 602, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 773, in _execute
return self._parent.execute(command, params)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unexpected command response
(Session info: chrome=103.0.5060.53)
....
Here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
service=Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.techwithtim.net/")
print(driver.title)
search = driver.find_element(By.NAME, 's')
search.send_keys("test")
search.send_keys(Keys.RETURN)
driver.quit()
I've been getting the same error as well. There seems to be an issue with Chromedriver 103. Your options are to wait for the new version, or to download Chrome Beta and use Chromedriver 104 with it.
A lot of people have also said to downgrade and use 102 instead.
Here's a link to a thread where others have reported the same problem:
https://github.com/SeleniumHQ/selenium/issues/10799
(//input[#name='s'])[2]
There's actually a second input with that value hidden use the above xpath.
Traceback (most recent call last):
File "Inventorytest.py", line 88, in <module>
j.go_to_application()
File "Inventorytest.py", line 65, in go_to_application
EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))
File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I got above exception with EC2 server instance. My script works absolutely fine with Ubuntu and Mac os with any version of firefox and geckodriver on local system. But got above error with EC2 ubuntu 18.04.01 version, in this I have also tried upgrade and downgrade firefox and geckodriver versions but still not work. Can anyone help me out with suggestion and solutions.
This error message...
Traceback (most recent call last):
File "Inventorytest.py", line 88, in <module>
j.go_to_application()
File "Inventorytest.py", line 65, in go_to_application
EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))
File "/home/naroladev/Mercury_Back-End/mercuryenv/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
...implies that the WebDriver variant was unable to locate the desired WebElement within the timeframe for which the WebDriverWait was constructed.
WebDriverWait
The WebDriverWait constructor takes a WebDriver instance as an argument and timeout in seconds.
Hence, irrespective of usage of either of the expected_conditions, WebDriverWait on failure will result in TimeoutException.
This usecase
In this usecase, the line:
EC.element_to_be_clickable((By.ID, 'FavoriteApp_ITEM'))
was unable to identify the desired element within the desired time frame hence you faced TimeoutException.
However, from TimeoutException it will be tough to dig out the actual result of the failure.
Solution
As a solution to know about the exact cause of the failure, you need to remove the WebDriverWait and replace the line of code with either:
find_element_by_class_name(name)
find_element_by_css_selector(css_selector)
find_element_by_id(id)
find_element_by_link_text(link_text)
find_element_by_name(name)
find_element_by_partial_link_text(partial_link_text)
find_element_by_tag_name(tag_name)
find_element_by_xpath(xpath)
If required you can slow down the search inducing waits through time.sleep(secs) while debugging.
References
You can find a couple of relevant discussions in:
selenium.common.exceptions.TimeoutException while invoking .click() on an element through expected_conditions
ExpectedConditions.ElementIsVisible returns TimeoutException even when element is present
This question already has an answer here:
"unknown error: cannot read property 'scrollleft' of null" in Chrome using selenium
(1 answer)
Closed 3 years ago.
I'm trying to click on "retweet" with selenium.
I found this way with javascript:
document.querySelector('[data-testid="retweet"]').click()
document.querySelector('[data-testid="retweetConfirm"]').click()
So I implemented in selenium with something like this:
firstJS = "document.querySelector('[data-testid=\"retweet\"]').click()"
secondJS ="document.querySelector('[data-testid=\"retweetConfirm\"]').click()"
time.sleep(5)
driver.execute_script(firstJS)
time.sleep(3)
driver.execute_script(secondJS)
and i get this error:
Traceback (most recent call last):
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 75, in <module>
main()
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 67, in main
retweet(link, driver)
File "C:\Users\Riccardo\Desktop\ProxyGiveaway\config.py", line 43, in retweet
driver.execute_script(firstJS)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Riccardo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'click' of null
(Session info: chrome=1.2.3.4)
EDIT: I added some time.sleep() and the first line works, but not the second
This might be due to page loading try adding wait until page load.
I have a problem with selenium with python 3.5, after install all the windows updates all my selenium script broken, I receive every time the same error:
Traceback (most recent call last):
File "C:/Users/Carlo/Desktop/CEx/src/IE.py", line 12, in
a=driver.find_element_by_xpath("//*[#id='un']")
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to find element on closed window
But I really don't understand why because the window of IE is open!
This is my code (simple just to check why I can't make it work more):
import time from
selenium import webdriver
driver = webdriver.Ie()
driver.get('http://gala.test-platform.celtrino.com/Login.aspx')
time.sleep(10)
driver.find_element_by_xpath(".//*[#id='un']")
The code fail every time in the last line and I double check with firepath and the xpath is correct so I don't really understand why it's not working.
I knew, IE has problems with xpath. It does not support xpath directly. It needs third party tools to do this. So, I suggest you to try cssSelector or any other options instead. Since, the element has an ID so you could use this. It's better.
driver.find_element_by_id("un");
I'm currently in CH11 from the "Automate the Boring Stuff with Python" book and I'm going over the Selenium module. I'm trying to move to the end of a page but I'm getting some problems. I also tried to look similar problems in this site and tried the solutions suggested without success unfortunately. Here's my code, when I type it into the IDLE Shell:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser= webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem= browser.find_element_by_tag_name('html')
type(htmlElem)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>
htmlElem.send_keys(Keys.END) # Error
Exception -:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
htmlElem.send_keys(Keys.END)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not visible
Apparently the html element is not visible? I don't understand how so since it seems to locates the html element just fine as seen on the code without any problems but the Key.ENTER is where I'm getting the error.
Any help would be appreciated.
Just tested the following with Chrome driver and it works (It should also work with Firefox):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('http://nostarch.com')
body_elem = browser.find_element_by_tag_name('body')
body_elem.send_keys(Keys.END)