selenium 元素选择问题和VSCode错误?自动化无聊的东西第12章

ejk8hzay  于 2023-02-12  发布在  Vscode
关注(0)|答案(1)|浏览(125)

我尝试复制这个代码:

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方法似乎没有注册为方法。
    1.每次我运行这个Intellicode提示被禁用
    1.我根本无法在Chrome上运行这个,因为它会导致Chrome浏览器崩溃
    我也在网上搜索过驱动程序的问题,用chrome驱动程序路径做了webdriver,也不行
    这是我得到的错误,如果运行它没有尝试和除了。
    elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
pvcm50d1

pvcm50d11#

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")

但如果类名有多个段,请使用CSS选择器中的点标记:

driver.find_element("css selector", "img.cover-thumb")

相关问题