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! Show What Caused the ErrorI 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:
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 VersionThe 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!
If 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.
We need to consider the following pros and cons of downgrading a library: Pros
Cons
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.
Below are some pros and cons if we choose to upgrade to the latest version of Selenium: Pros
Cons
Additional ResourcesUse Python To Send WhatsApp Message Tejas Patoleunread, Nov 12, 2013, 3:37:52 AM11/12/13 to
Dan Cuellarunread, 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 Lippsunread, Nov 12, 2013, 12:03:48 PM11/12/13 to Tejas Patole, It's probably the brackets breaking the xpath regex parser Tejas Patoleunread, Nov 15, 2013, 1:10:47 AM11/15/13 to , Tejas Patole then is there any solution for this ? Jonathan Lippsunread, 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 Lippsunread, 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'). jwallisunread, 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} |