59
from selenium import webdriver
import time

test = webdriver.Chrome()
test.get('https://docs.google.com/forms/d/e/1FAIpQLSeYUmAYYZNtbU8t8MRxwJo-        d1zkmSaEHodJXs78RzoG0yFY2w/viewform')

time.sleep(5)

Name = 'kuch bhi'
last = test.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
last.send_keys(Name)

When i execute the code, I get an error that says,

AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

4 Answers 4

133

Selenium just removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout

You now need to use:

driver.find_element("xpath", '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')

In your example, you would use:

last = test.find_element("xpath", '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')

For improved reliability, you should consider using WebDriverWait in combination with element_to_be_clickable.

6
  • 1
    Thanks, It worked but where did you learn to do it the new way? I have just seen the documentation, it mentions the old methods Jun 25, 2022 at 16:10
  • 9
    I'm on the Selenium Technical Leadership Committee, where I spend time with the team online, and there are video calls occasionally. twitter.com/SeleniumBase/status/… (I'm in the loop when it comes to updates.) Jun 25, 2022 at 16:18
  • Oh! Please if possible in any way, just keep compatibility with older versions and/or include a depreciation warning in advance...
    – Peter
    Jul 18, 2022 at 21:12
  • 1
    There was a deprecation warning before that (assuming it wasn't being ignored). And I was not in favor of this decision by the higher leadership of the Selenium committee to remove compatibility with older scripts. I created SeleniumBase to do things a bit differently, and older methods don't get removed. Scripts from 2016 still work with the latest version. Jul 18, 2022 at 21:46
  • 1
    @Swannie For finding multiple elements and making a list: driver.find_elements(by=by, value=selector) Oct 5, 2022 at 15:19
22

You can now use:

from selenium.webdriver.common.by import By

driver.find_element(by=By.XPATH, value='//<your xpath>')
1
  • 3
    that's actually the perfect way to do it using XPath in selenium above 4 version Nov 24, 2022 at 12:05
7

As per the changelogs of Selenium 4.3.0:

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)

and as per the merge the 16 replaced strings are as follows:

.find_element_by_class_name(
.find_element(By.CLASS_NAME, 

.find_element_by_css_selector(
.find_element(By.CSS_SELECTOR, 

.find_element_by_id(
.find_element(By.ID, 

.find_element_by_link_text(
.find_element(By.LINK_TEXT, 

.find_element_by_name(
.find_element(By.NAME, 

.find_element_by_partial_link_text(
.find_element(By.PARTIAL_LINK_TEXT, 

.find_element_by_tag_name(
.find_element(By.TAG_NAME, 

.find_element_by_xpath(
.find_element(By.XPATH, 

.find_elements_by_class_name(
.find_elements(By.CLASS_NAME, 

.find_elements_by_css_selector(
.find_elements(By.CSS_SELECTOR, 

.find_elements_by_id(
.find_elements(By.ID, 

.find_elements_by_link_text(
.find_elements(By.LINK_TEXT, 

.find_elements_by_name(
.find_elements(By.NAME, 

.find_elements_by_partial_link_text(
.find_elements(By.PARTIAL_LINK_TEXT, 

.find_elements_by_tag_name(
.find_elements(By.TAG_NAME, 

.find_elements_by_xpath(
.find_elements(By.XPATH,

This usecase

So effectively you have to replace the line of code:

last = test.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')

as:

last = test.find_element(By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')

Note

You will also need to import By as follows:

from selenium.webdriver.common.by import By
0

e = driver.find_element(by.By.XPATH,'//label[@analytics-event="All matches"]') from selenium.webdriver.common import by

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.