About the new version of selenium positioning element error: 'WebDriver' object has no attribute 'find_element_by_id' and other issues

Since I haven’t used Selenium for a period of time, when I used it again, I found that the previously written Selenium element positioning code would report an error after running. It was found that after Selenium was updated to a new version (version 4.x), the syntax of some commonly used codes in the past occurred. Change, of course, if you have not updated or downloaded the latest version of Selenium, it will not be affected, and you can still use the previous wording. The next step is to discuss the new syntax of Selenium positioning element code after the new version.

Change 1: executable_path

Old version Selenium code:

from selenium import webdriver
driver=webdriver.Chrome(executable_path='/home/yan/Python/chromeselenium/chromeselenium/chromedriver')

executable_path is the storage path of our Selenium driver. Selenium can work normally only if the executable_path is used to specify the path. However, after the Selenium version is updated, when using the above method, the system will report an error executable_path has been deprecated, please pass in a Service object ,As follows:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path="/home/yan/Python/chromeselenium/chromeselenium/chromedriver")

It means: executable_path has been deprecated, please pass in a Service object, so we need to modify it to the following code:

New version Selenium code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service  # 新增

service = Service(executable_path='/home/yan/Python/chromeselenium/chromeselenium/chromedriver')

driver = webdriver.Chrome(service=service)

driver.get("网址")

Change 2: Selenium locates the element code

In the old version, most of us used the following code to locate elements.
Old version Selenium element positioning code:

# 以下inputTag任选其一,其他注释掉
inputTag = driver.find_element_by_id("value")  # 利用ID查找

inputTags = driver.find_element_by_class_name("value")  # 利用类名查找

inputTag = driver.find_element_by_name("value")  # 利用name属性查找

inputTag = driver.find_element_by_tag_name("value")  # 利用标签名查找

inputTag = driver.find_element_by_xpath("value")  # 利用xpath查找

inputTag = driver.find_element_by_css_selector("value")  # 利用CSS选择器查找

Before the version was updated, we used the driver.find_element_by_ method name ("value") , the method name is by_id, by_class_name, by_name, etc., and "value" is the value passed in , taking the Baidu search box as For example, right-click the Baidu search box and click Check to see the attribute id=”kw” in its HTML source code . Using the id value to find the search box in the old version should be:

inputTag = driver.find_element_by_id("kw")

insert image description here

Before the version is updated, usually the operation can correctly locate the corresponding element, but after the version upgrade of Selenium, an error will be reported after the operation. Take driver.find_element_by_id("value") as an example (other errors are also similar to the following error message), and an error will be reported after running, as follows:
insert image description here

According to the latest official document, the code is modified. The modified format is changed from driver.find_element_by_method name("value") to driver.find_element(By.method name, "value") . The specific changes are as follows:

New version of Selenium code:
first introduce the following code in the file header

from selenium.webdriver.common.by import By

Then make the following changes:

# inputTag = driver.find_element_by_id("value")  # 利用ID查找
# 改为:
inputTag = driver.find_element(By.ID, "value")

# inputTags = driver.find_element_by_class_name("value")  # 利用类名查找
# 改为:
inputTag = driver.find_element(By.CLASS_NAME, "value")

# inputTag = driver.find_element_by_name("value")  # 利用name属性查找
# 改为:
inputTag = driver.find_element(By.NAME, "value")

# inputTag = driver.find_element_by_tag_name("value")  # 利用标签名查找
# 改为:
inputTag = driver.find_element(By.TAG_NAME, "value")

# inputTag = driver.find_element_by_xpath("value")  # 利用xpath查找
# 改为:
inputTag = driver.find_element(By.XPATH, "value")

# inputTag = driver.find_element_by_css_selector("value")  # 利用CSS选择器查找
# 改为:
inputTag = driver.find_element(By.CSS_SELECTOR, "value")

After the modification, you can use selenium to automate the work!

Guess you like

Origin blog.csdn.net/m0_49076971/article/details/126233151
Recommended