DepricationWarning when using Webdriver - python

When I run selenium, I sometimes get this depricationwarning which is really weird. The error message doesn't display alot of information about the warning. Is there a fix?
Error:
Traceback (most recent call last):
File "main.py", line 4, in <module>
main.py:15: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
driver.find_element_by_name("gameId").send_keys(gamepin+Keys.ENTER)
main.py:16: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
driver.find_element_by_id("nickname").send_keys(username+Keys.ENTER)
Traceback (most recent call last):
File "main.py", line 16, in <module>
driver.find_element_by_id("nickname").send_keys(username+Keys.ENTER)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 472, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, 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="nickname"]"}
(Session info: chrome=91.0.4472.101)
Stacktrace:
#0 0x55f607e05919 <unknown>

Use the newer one since driver.find_element_by is depreciated.
driver.find_element(By.ID,"nickname").send_keys(username+Keys.ENTER)
What I would recommended is using Webdriver waits if it's after page loading.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"nickname"))).send_keys(username+Keys.ENTER)
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Related

Traceback (most recent call last): File "D:\pythonProject\webscraping.py", line 17, in <module> search.send_keys(Keys.RETURN) Error python

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.

raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: '' [duplicate]

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

selenium.common.exceptions.TimeoutException error using WebDriverWait with expected_conditions through Selenium and Python

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

errors with webdriver.Firefox() with selenium

I am using python 3.5, firefox 45 (also tried 49) and selenium 3.0.1
I tried:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
Then I got the error message:
C:\Users\A\AppData\Local\Programs\Python\Python35\python.exe
C:/Users/A/Desktop/car/test.py
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/A/Desktop/car/test.py", line 4, in <module>
driver = webdriver.Firefox()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0000000000EB8278>>
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
What can I do? Any help is much appreciated!
If you are using firefox ver >47.0.1 you need to have the [geckodriver][1] executable in your system path. For earlier versions you want to turn marionette off. You can to so like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['marionette'] = False
driver = webdriver.Firefox(capabilities=capabilities)

Selenium - Element is Not Visible [Python]

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)

Categories