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

from selenium import webdriver import time test = webdriver.Chrome() test.get(' 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

Selenium just removed that method in version 4.3.0. See the 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.

7

You can now use:

from selenium.webdriver.common.by import By driver.find_element(by=By.XPATH, value='//<your xpath>') 
1

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 

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like