Find_Element_By_* Commands Are Deprecated in Selenium

find_element_by_* commands are deprecated in Selenium

This error message...

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead

...implies that the find_element_by_* commands are deprecated in the latest Selenium Python libraries.

As AutomatedTester mentions: This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages and this does that.



Solution

Instead you have to use find_element(). As an example:

You have to include the following imports

from selenium.webdriver.common.by import By
  • Using class_name:

    button = driver.find_element_by_class_name("quiz_button")

    Needs be replaced with:

    button = driver.find_element(By.CLASS_NAME, "quiz_button")

Along the lines of, you also have to change the following:

  • Using id:

    element = find_element_by_id("element_id")

    Needs be replaced with:

    element = driver.find_element(By.ID, "element_id")
  • Using name:

    element = find_element_by_name("element_name")

    Needs be replaced with:

    element = driver.find_element(By.NAME, "element_name")
  • Using link_text:

    element = find_element_by_link_text("element_link_text")

    Needs be replaced with:

    element = driver.find_element(By.LINK_TEXT, "element_link_text")
  • Using partial_link_text:

    element = find_element_by_partial_link_text("element_partial_link_text")

    Needs be replaced with:

    element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
  • Using tag_name:

    element = find_element_by_tag_name("element_tag_name")

    Needs be replaced with:

    element = driver.find_element(By.TAG_NAME, "element_tag_name")
  • Using css_selector:

    element = find_element_by_css_selector("element_css_selector")

    Needs be replaced with:

    element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
  • Using xpath:

    element = find_element_by_xpath("element_xpath")

    Needs be replaced with:

    element = driver.find_element(By.XPATH, "element_xpath")

Note: If you are searching and replacing to implement the above changes, you will need to do the same thing for find_elements_*, i.e., the plural forms of find_element_*.

You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: Upgrade to Selenium 4

Python Selenium warning DeprecationWarning: find_element_by_* commands are deprecated

Upgrading your Python version will not help solve this issue, since find_element is a Selenium-specific function.

driver.find_element_by_* has been deprecated in Selenium 4 newer version.

So you should be using

driver.find_element(By.XPATH, "(//span[@class='table-number'])[1]").text

The first one that you are using:

my_table = driver.find_element(By.XPATH, ("(//span[@class='table-number'])[1]").text

has an extra (.

And the second one

my_table = driver.find_element(By.XPATH, "(//span[@class='table-number'])[1]").text

seems correct.

Unable to locate elements in Selenium (Python)

As per your comment, you are using Selenium WebDriver version 4.0.0a5 which is not stable though. There is the potential that features may be added/removed between these releases. You may switch back to 3.141.59 and give it a try:

Sample Image

driver = webdriver.Chrome(executable_path="C:\New folder\chromedriver.exe",chrome_options=chrome_options)
url = 'http://www.youtube.com'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)

element = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='search-form']//div[@id='container']//div[@id='search-input']//input")))

actionChains = ActionChains(driver)
actionChains.move_to_element(element).click().perform()
actionChains.move_to_element(element).send_keys("Test",Keys.RETURN).perform()

Note: please add the below imports to your solution

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead using Selenium in Google Colab

These DeprecationWarning logs...

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
...
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
...
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead

...is the result of the change in the latest version of Selenium which is inline with the Selenium 4 Release Candidate 1 changelog which mentions:

Specify that the "find_element_by_* ..." warning is a deprecation warning (#9700)



Solution

Instead of find_element_by_* you have to use find_element(). As an example:

You need to add the following import:

from selenium.webdriver.common.by import By
  • Instead of using:

    takimlar = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[3]")
  • You need to use:

    takimlar = driver.find_elements(By.XPATH, "//table[@id='predictions_table']/tbody/tr/td[3]")


References

You can find a couple of relevant detailed discussions in:

  • What is the difference between "find_element_by_name('name')" and "find_element(By.NAME, 'name')"?
  • Unable to locate elements in Selenium (Python)

DeprecationWarning: find_element_by_* commands are deprecated

You need to add the following import:

from selenium.webdriver.common.by import By

Then replace this:

driver.find_element_by_xpath("//*[@title='Consent all cookies']").click()

with this:

driver.find_element(By.XPATH, "//*[@title='Consent all cookies']").click()

Python Selenium, Error with find_element_by() command

find_element_by_* methods are now deprecated.

find_element(By. are used now.

So, instead of find_element_by_name it is a driver.find_element(By.NAME, "name_attribute") now.

Similarly driver.find_element(By.XPATH, "element_xpath_locator") etc.

To use these methods you will need the following import:

from selenium.webdriver.common.by import By


Related Topics



Leave a reply



Submit