错误:Web 驱动程序对象没有属性"find_element_by_class_name"



我正在使用selenium与chromedriver,并在尝试与特定元素交互时获得错误。

HTML类代码:<div class="label-31sIdr">Fresh</div>

Selenium Python代码:

from selenium import webdriver
import requests
import time
path = '/Users/stryker/Downloads/chromedriver'
driver = webdriver.Chrome(path)
payload = {
'content': "pls pm"
}
header = {
'authorization': 'xxxxx'
}
for i in range (1000):
r = requests.post('https://discord.com/api/v9/channels/991579455376072775/messages', data=payload, headers=header)
time.sleep(1.5)
button = driver.find_element_by_class_name('Fresh')
button.click()
time.sleep(30)

错误:

line 19, in <module>
button = driver.find_element_by_class_name('Fresh')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'

Selenium刚刚在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

你现在需要使用:

from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME, THE_CLASS_NAME)

或者没有额外的导入:

driver.find_element("class name", THE_CLASS_NAME)

为提高可靠性,应考虑将WebDriverWaitelement_to_be_clickable结合使用。

另一个问题是你提到的HTML是:<div class="label-31sIdr">Fresh</div>但是当label-31sIdr是类名时,您试图使用Fresh作为类名。的代码是:

driver.find_element("class name", "label-31sIdr")

相关内容

  • 没有找到相关文章

最新更新