簡體   English   中英

Selenium 元素選擇問題和 VSCode 錯誤? 自動化無聊的事情第 12 章

[英]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 並且幾乎沒有什么問題。

  1. find_element_by_class_name 方法似乎沒有注冊為方法。
  2. 每次我運行此 Intellicode 提示時都會被禁用
  3. 我根本無法在 Chrome 上運行它,因為它會使 Chrome 瀏覽器崩潰

我還在網上搜索了驅動程序問題,並用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM