. Advertisement .
..3..
. Advertisement .
..4..
Sometimes, you try to execute your codes, and the error “AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’” comes across your work. If you are seeking solutions for this error, we are here to provide you with information and methods to fix this issue.
Why Does The Error “AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’” Occur?
The Error “AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’” appears from Selenium version 4.3.0. A few months ago, Selenium released a new Selenium version – version 4.3.0. This new version has a couple of changes; one of those is the API we used to work with find_element_by_* and find_elements_by_* are now removed according to their Official Document. The announcement is as follows.
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
So if you update Selenium to the latest version and run find_element_by_* or find_elements_by_* to locate WebElement, the command can not execute it, and the error will show up as below.
AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath'
The following section of this article will give you options of the solution to resolve this error.
How To Solve The Error “AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’”?
METHOD 1: Use find_element
As per Selenium Official Document, the usual API find_element_by_* and find_elements_by_* are not in use anymore. Instead of using the old API find_elements_by_xpath, you should change to the new API find_element. You can use it as follows.
driver.find_element("name", "q")
For example, you can run it like this
driver.find_element(By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
Or like this
driver.find_element("xpath", '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
This solution is great due to its ease and efficiency, but there might be a problem if you are working on a large project. The time-consuming of re-write the code or Find and Replace would make you consider using this method or the next method we are going to show you.
Leave us your comments below if this solution helps you to fix the problem. And if this solution does not suit you, the following one will do it.
METHOD 2: Downgrade Selenium Version
As mentioned, the error “AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’” happens from Selenium version 4.3.0 and prior. Because of that, installing the earlier version will solve this problem. To downgrade Selenium, you need to use pip with an argument –force-reinstall, including the name of the version you want to install. Run the snippet below on the current version.
pip install selenium==4.2.0 --force-reinstall
After that, you will face a warning message as follows. Ignore it, and the program will run fine.
Warning (from warnings module):
File "<pyshell#8>", line 1
DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead
This message appears because you should not use the old version.
One disadvantage you will face is that the library may not offer the latest features or support.
Conclusion
“AttributeError: ‘WebDriver’ object has no attribute ‘find_elements_by_xpath’” is an error in Selenium 4.3.0 when you try to run find_element_by_* and find_elements_by_*. Above are the solutions for you to resolve this problem. We hope that our methods will help you fix the issue. Leave a comment below if you find any trouble while fixing the error or have questions for us. Thank you for reading.
Read more:
→ AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_name’ – How To Solve It?
Leave a comment