AttributeError:“WebDriver”对象没有属性“find_element_by_xpath”

fir*_*r47 75 selenium browser-automation python-3.x selenium-chromedriver selenium-webdriver

from selenium import webdriver
import time

test = webdriver.Chrome()
test.get('https://docs.google.com/forms/d/e/1FAIpQLSeYUmAYYZNtbU8t8MRxwJo-        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)
Run Code Online (Sandbox Code Playgroud)

当我执行代码时,我收到一条错误消息,

AttributeError:“WebDriver”对象没有属性“find_element_by_xpath”

Mic*_*ntz 158

Selenium 刚刚在 version 中删除了该方法4.3.0。查看更改:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/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
Run Code Online (Sandbox Code Playgroud)

您现在需要使用:

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
Run Code Online (Sandbox Code Playgroud)

在您的示例中,您将使用:

driver.find_element("xpath", '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
Run Code Online (Sandbox Code Playgroud)

为了提高可靠性,您应该考虑WebDriverWait与 结合使用element_to_be_clickable

  • 我是 Selenium 技术领导委员会的成员,我在那里与团队在线交流,偶尔也会进行视频通话。https://twitter.com/SeleniumBase/status/1448685115610214406?s=20&t=tx5-m9d_ea6PHpgsHEZXJA(当涉及到更新时,我正在循环中。) (10认同)
  • 在此之前有一个弃用警告(假设它没有被忽略)。我并不赞成 Selenium 委员会高层领导做出的取消与旧脚本兼容性的决定。我创建了 [SeleniumBase](https://github.com/seleniumbase/SeleniumBase) 来做一些不同的事情,并且旧的方法不会被删除。2016 年的脚本仍然适用于最新版本。 (2认同)
  • @Swannie用于查找多个元素并制作列表:``driver.find_elements(by=by, value=selector)`` (2认同)

Uri*_*Uri 27

您现在可以使用:

from selenium.webdriver.common.by import By

driver.find_element(by=By.XPATH, value='//<your xpath>')
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是在 selenium 4 以上版本中使用 XPath 的完美方法 (3认同)

Deb*_*anB 19

根据Selenium 4.3.0 的变更日志

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
Run Code Online (Sandbox Code Playgroud)

根据合并, 16 个替换字符串如下:

.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,
Run Code Online (Sandbox Code Playgroud)

这个用例

因此,您必须有效地替换以下代码行:

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')
Run Code Online (Sandbox Code Playgroud)

作为:

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')
Run Code Online (Sandbox Code Playgroud)

笔记

您还需要By按如下方式导入:

from selenium.webdriver.common.by import By
Run Code Online (Sandbox Code Playgroud)