Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_element_by_* commands are deprecated in selenium

When starting the function

def run(driver_path):    
    driver = webdriver.Chrome(executable_path=driver_path)    
    driver.get('https://tproger.ru/quiz/real-programmer/')    
    button = driver.find_element_by_class_name("quiz_button")
    button.click()
run(driver_path)

I'm getting errors like these:

<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path=driver_path)
<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  button = driver.find_element_by_class_name("quiz_button")

... but I can't understand why.

I'm using webdriver at the latest version for my chrome's version. I don't why I get

`find_element_by_* commands are deprecated` 

... when it's in the documentation that the command exists.

like image 504
Alexandr Avatar asked Nov 07 '21 17:11

Alexandr


People also ask

Is find_element_by_* deprecated in selenium?

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.

How to resolve selenium find elements by partial link text error?

Selenium Find elements by Partial link text In this Python program example, to resolve this error find_element_by_* commands are deprecated to resolve it while using find_element_by_id we have to use find_element by using the following steps. For this, First, need to import "from selenium.webdriver.common.by import By"

What are selenium locators and how do I use them?

Selenium offers a number of built-in locator strategies to uniquely identify an element. There are many ways to use the locators in very advanced scenarios. For the purposes of this documentation, let’s consider this HTML snippet:

When should I use find_elements () instead of remove the CWD?

Please use find_elements () instead after removing the cwd from sys.path. /usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: DeprecationWarning: find_elements_by_* commands are deprecated.


Video Answer


1 Answers

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")
    

In similar lines 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: https://www.selenium.dev/documentation/webdriver/getting_started/upgrade_to_selenium_4/

like image 64
undetected Selenium Avatar answered Sep 21 '22 13:09

undetected Selenium