. Advertisement .
..3..
. Advertisement .
..4..
“AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’” is an error when you use “find_element_by_css_selector”. This article will give you information and solutions to this error.
How Does The Error “AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’” Occur?
Locating elements on websites has always been a nightmare because of the sophistication and entanglement of finding and automating those website elements. To reduce its complexity, Selenium Software will help us with this task. find_element_by_css_selector, a method syntax in Selenium to locate website elements.
Suppose the error appears when you try to locate website elements by find_element_by_css_selector in Python. Following is the snippet of the error.
AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector'
From Selenium version 4.3.0, the API find_element_by_* and find_elements_by_* is deprecated. The API of the latest version has changed to new fluent syntax drive.find_element() instead of drive.find_element_by_css_selector.
How To Resolve The Error “AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’”?
METHOD 1: Change to “find_element()”
As mentioned, using syntax find_element() will help you quickly solve the error “AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’”. You can use this just like down below.
driver.find_element("name", "q")
For example, you use find_element 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
driver.find_element("xpath", '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
And you are done. You will not face this error next time.
But if you do not want to change your code, there are still more solutions for you as follows.
METHOD 2: Upgrade New Codes
Install the latest Selenium version, and upgrading your new codes will be not so bad. You need to copy this whole snippet and paste it into your program.
find_element(By.ID, "id")
find_element(By.NAME, "name")
find_element(By.XPATH, "xpath")
find_element(By.LINK_TEXT, "link text")
find_element(By.PARTIAL_LINK_TEXT, "partial link text")
find_element(By.TAG_NAME, "tag name")
find_element(By.CLASS_NAME, "class name")
find_element(By.CSS_SELECTOR, "css selector")
The pros of using the latest version are that you can use the latest features and have full support.
METHOD 3: Install Previous Selenium Versions
If you still want to use your codes like before, you need to install an older version of Selenium, such as Selenium version 4.2.0 or earlier. A warning message will appear, ignore it, and your program will run fine. But you better use the new one because this means the approach syntax drive.find_element_by_css_selector will no longer suitable.
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
If you are fine with this old version, downgrading the library using pip with an argument –force-reinstall including the version you want to use.
pip install selenium==4.2.0 --force-reinstall
With this method, you will not need to change your code, and it is easier for you to code again. But the disadvantage of this method is the library may not offer you the latest features or support.
We hope that the solutions we give you will help to fix the error you are facing without any problem.
Conclusion
“AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_css_selector’” is an error on Selenium version 4.3.0 when you still using ‘find_element_by_css_selector’ as the earlier version. This error would be resolved with the three solutions above, and you can continue with your programming. What do you think, and do you have questions about this article? Please leave a comment below for us. Thank you!
Read more
→ Tips to Fix The Error “AttributeError: ‘str’ object has no attribute ‘decode’”
Leave a comment