0

Okay, so I am working on my laptop rn and I copy and pasted my code from my PC and suddenly it's not working. I have installed the same Selenium but now it is acting up, I am receiving DeprecationWarnings, driver.find_element_by_xpath isn't working etc.

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", chrome_options=options_)
    driver.get("http://www.discord.com")
    driver.find_element_by_xpath()

This is an example of what I have written which is no longer working, the driver.find_element_by_xpath() has a line through it! And when I rewrite this in another py file (in pycharm) it does not like me using driver, it comes up underlined in red.

Can somebody explain what on earth is going on?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

This error message...

 DeprecationWarning: find_element_by_* commands are deprecated

...implies that the find_element_by_* commands are now associated with a DeprecationWarning in the latest Selenium Python libraries.


This change is inline with the Selenium 4 Release Candidate 1 changelog which mentions:

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


compatible code

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

  • Using xpath:

    driver.find_element_by_xpath("element_xpath")
    

    Needs be replaced with:

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

You need to add the following import:

from selenium.webdriver.common.by import By

Additionally, you have to use options instead of chrome_options as chrome_options is deprecated now.

You can find a couple of relevant detailed discussion in:


Solution

Effectively, your code block will be:

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", options=options_)
    driver.get("http://www.discord.com")
    driver.find_element(By.XPATH, "element_xpath")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Now you need to use this:

from selenium import webdriver
from selenium.webdriver.common.by import By  

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", 
    chrome_options=options_)
    driver.get("http://www.discord.com")
    driver.find_element(By.XPATH, 'your xpath')

This works for By.CLASS_NAME, By.CSS_SELECTOR and etc