如何修复“WebDriver”对象没有“通过 CSS 选择器查找元素”属性?

我正在使用一个名为 callow 的蛮力脚本,我试图在一个免费的沙箱上对其进行测试(因为我正在学习道德黑客),当我运行脚本时,我得到了这些错误:

Traceback (most recent call last):
  File "C:UsersJoecallowcallow.py", line 163, in <module>
    wizard()
  File "C:UsersJoecallowcallow.py", line 93, in wizard
    brutes(username, username_selector, password_selector, submit_selector, pass_list, website)
  File "C:UsersJoecallowcallow.py", line 119, in brutes
    Sel_user = browser.find_element_by_css_selector(username_selector) # Finds Selector
AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector'

这是我的代码中似乎出现错误的部分。

try:
        Sel_user = browser.find.element_by_css_selector(username_selector) # Finds Selector
    except selenium.common.exceptions.NoSuchElementException:
        print((color.RED + 'n[!]'+ color.CWHITE + ' Username feild selector is invalid.'))
        sys.stdout.flush()
        exit()
    try:
        Sel_pas = browser.find_element_by_css_selector(password_selector) # Finds Selector
    except selenium.common.exceptions.NoSuchElementException:
        print((color.RED + 'n[!]'+ color.CWHITE + ' Password feild selector is invalid.'))
        sys.stdout.flush()
        exit()
    try:
        enter = browser.find.element_by_css_selector(submit_selector) # Finds Selector
    except selenium.common.exceptions.NoSuchElementException:
        print((color.RED + 'n[!]'+ color.CWHITE + ' Login button selector is invalid.'))
        sys.stdout.flush()
        exit()

我怎样才能解决这个问题?

stack overflow How to fix 'WebDriver' object has no attribute 'find element by CSS selector'?
原文答案

答案:

作者头像

既然它提到了WebDriver,我假设这与Selenium有关? find_element_by_css_selector 已弃用,如果您要使用像 PyCharm 这样的 IDE,它会告诉您:

find_element_by_css_selector 已弃用。请改用 find_element(by=By.CSS_SELECTOR, value=css_selector)

因此,不要使用 find_element_by_css_selector ,而是考虑使用:

Sel_user = browser.findElement(By.cssSelector, username_selector)

更多信息请参考官方文档: https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-css-selectors

相关问题