If you are trying to fix the Selenium AttributeError: ‘WebDriver’ object has no attribute ‘find_element_by_xpath’, then you are at the right place!
What Caused the Error
I recently upgraded selenium to version 4.3.0., then started to have this problem “WebDriver” object has no attribute “find_element_by_xpath”. This is because starting from version 4.3.0., selenium has changed the usual API driver.find_element_by_xxx. Instead, the new method syntax is now simply driver.find_element(by_what, element). For example:
driver.find_element('xpath', '//*[@id="main"]/footer/div[1]/div/span[2]') ## OR from selenium.webdriver.common.by import By driver.find_element(By.Name, 'name')Now we know the root cause of the error message, and fixing it is easy. We’ll look at two approaches.
Fix # 1 – Install An Older Selenium Version
The old API still works in earlier versions 4.2.0 and prior. If we try to run the old API in those earlier versions, we’ll get a warning message, but the program still runs okay. This is telling us that the old approach find_element_by_xxx is going away, better start using the new one!
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) insteadIf you are okay with using an older version of selenium, you can downgrade the library using pip with an argument –force-reinstall, as well as specifying which version we want to use. The below will overwrite our current selenium, then install version 4.2.0 instead.
pip install selenium==4.2.0 --force-reinstallWe need to consider the following pros and cons of downgrading a library:
Pros
- Easier to get code to work again
- No coding changes required
Cons
- Have to re-write our code
- May not get the latest support or the latest features the library has to offer
Fix # 2 – Update Our Code (And Use the Latest Version of Selenium)
If the decision is to upgrade to the latest selenium, then we’ll have to update the code, which might not be that bad. Doing find+replace a few times should update all the code for us.
Below is a comparison between the old and new APIs for finding web elements, as we can see the new API is now just find_element(), then we specify what element we want to find as the first argument.
Old API | New API |
find_element_by_id(‘id’) | find_element(By.ID, ‘id’) |
find_element_by_name(‘name’) | find_element(By.NAME, ‘name’) |
find_element_by_xpath(‘xpath’) | find_element(By.XPATH, ‘xpath’) |
find_element_by_link_text(‘link_text’) | find_element(By.LINK_TEXT, ‘link_text’) |
find_element_by_partial_link_text(‘partial_link_text’) | find_element(By.PARTIAL_LINK_TEXT, ‘partial_link_text’) |
find_element_by_tag_name(‘tag_name’) | find_element(By.TAG_NAME, ‘tag_name’) |
find_element_by_class_name(‘class_name’) | find_element(By.CLASS_NAME, ‘class_name’) |
find_element_by_css_selector(‘css_selector’) | find_element(By.CSS_SELECTOR, ‘css_selector’) |
Below are some pros and cons if we choose to upgrade to the latest version of Selenium:
Pros
- Latest features, and full support
- Future-proof
Cons
- Need to re-write code, which can be a lot of work for large projects
Additional Resources
Use Python To Send WhatsApp Message
Tejas Patole
unread,
Nov 12, 2013, 3:37:52 AM11/12/13
to
Hello All, In android app I have a TextView with text as "[PC]". and I am trying to find that element in python like following this self.driver.find_element_by_xpath("//TextView[@text='[PC]']"). but it gives error Message: u'An error occurred while searching for an element by XPath.' Thanks, Tejas |
Dan Cuellar
unread,
Nov 12, 2013, 11:25:18 AM11/12/13
to
full path is coming in appium 1.0, @text=… is not supported right now, in the meantime I'd recommend chaining you find element commands for this kind of stuff
Jonathan Lipps
unread,
Nov 12, 2013, 12:03:48 PM11/12/13
to Tejas Patole,
It's probably the brackets breaking the xpath regex parser
Tejas Patole
unread,
Nov 15, 2013, 1:10:47 AM11/15/13
to , Tejas Patole
then is there any solution for this ?
Jonathan Lipps
unread,
Nov 15, 2013, 10:46:07 AM11/15/13
to Tejas Patole,
nope. we'll be upgrading our xpath support soon so i'm not going to put any more work into making the current "parser" more robust.
Jonathan Lipps
unread,
Nov 15, 2013, 10:46:38 AM11/15/13
to Tejas Patole,
Anyway it's something you could work around by doing @contains(@text, 'PC').
jwallis
unread,
Nov 20, 2013, 5:01:32 PM11/20/13
to , Tejas Patole
Not asking you to do so, just noting that equals sign also has an issue. char previous to '=' and everything past it are removed from the search string.
without '='
2013-11-20T21:47:33.763Z - info: [BOOTSTRAP] [debug] Finding //text[@text='asdfpoiu'] using XPATH with the contextId:
2013-11-20 15:47:33,764 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [info] Got command of type ACTION [service.py:47]
2013-11-20T21:47:33.763Z - info: [BOOTSTRAP] [info] Building xpath selector from attr text and constraint asdfpoiu and substr false
2013-11-20T21:47:33.763Z - info: [BOOTSTRAP] [info] s0.className('android.widget.TextView').text('asdfpoiu')
2013-11-20 15:47:33,764 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [debug] Got command action: find [service.py:47]
2013-11-20 15:47:33,764 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [debug] Finding //text[@text='asdfpoiu'] using XPATH with the contextId: [service.py:47]
2013-11-20 15:47:33,765 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [info] Building xpath selector from attr text and constraint asdfpoiu and substr false [service.py:47]
2013-11-20 15:47:33,765 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [info] s0.className('android.widget.TextView').text('asdfpoiu') [service.py:47]
2013-11-20T21:47:33.839Z - info: [BOOTSTRAP] [info] Returning result: {"value":"Could not find an element using supplied strategy. ","status":13}
with '='
2013-11-20T21:48:44.802Z - info: [BOOTSTRAP] [debug] Finding //text[@text='asdf=poiu'] using XPATH with the contextId:
2013-11-20 15:48:44,802 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [debug] Finding //text[@text='asdf=poiu'] using XPATH with the contextId: [service.py:47]
2013-11-20T21:48:44.803Z - info: [BOOTSTRAP] [info] Building xpath selector from attr text and constraint asd and substr false
2013-11-20 15:48:44,804 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [info] Building xpath selector from attr text and constraint asd and substr false [service.py:47]
2013-11-20T21:48:44.808Z - info: [BOOTSTRAP] [info] s0.className('android.widget.TextView').text('asd')
2013-11-20 15:48:44,808 [DEBUG] mats.exec_logger.AppiumServer:info: [BOOTSTRAP] [info] s0.className('android.widget.TextView').text('asd') [service.py:47]
2013-11-20T21:48:44.818Z - info: [BOOTSTRAP] [info] Returning result: {"value":"Could not find an element using supplied strategy. ","status":13}