![](/img/trans.png)
[英]Automate The boring Stuff - chapter 11 - Filling out and submitting forms p. 259
[英]Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12
我正在嘗試復制此代碼:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://inventwithpython.com')
try:
elem = browser.find_element_by_class_name(' cover-thumb')
print('Found <%s> element with that class name!' % (elem.tag_name))
except:
print('Was not able to find an element with that name.')
但它不斷返回異常。 我在 mac w 上運行這個。 vscode 並且幾乎沒有什么問題。
我還在網上搜索了驅動程序問題,並用 chrome 驅動程序路徑完成了 webdriver。 也沒用
這是我在沒有嘗試和排除的情況下運行它時遇到的錯誤。
elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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)
* ...
您現在需要使用:
driver.find_element("class name", "VALUE_OF_CLASS_NAME")
例如:
driver.find_element("class name", "cover-thumb")
但是如果 class 名稱有多個段,請使用 CSS 選擇器中的點表示法:
driver.find_element("css selector", "img.cover-thumb")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.