【问题标题】:How to fix 'WebDriver' object has no attribute 'find element by CSS selector'?如何修复 \'WebDriver\' 对象没有属性 \'通过 CSS 选择器查找元素\'?
【发布时间】:2022-12-12 07:13:32
【问题描述】:

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

Traceback (most recent call last):
  File "C:\Users\Joe\callow\callow.py", line 163, in <module>
    wizard()
  File "C:\Users\Joe\callow\callow.py", line 93, in wizard
    brutes(username, username_selector, password_selector, submit_selector, pass_list, website)
  File "C:\Users\Joe\callow\callow.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()

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

【问题讨论】:

    标签: python brute-force


    【解决方案1】:

    因为它提到了 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

    【讨论】:

    • 对于阅读本文的任何人,python 的正确语法如下:browser.find_element(By.CSS_SELECTOR, username_selector)
    【解决方案2】:

    请不要忘记导入 By 对象以访问 CSS_SELECTOR 属性。我必须导入以下内容才能在 Chrome 浏览器中自动执行任务。

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.common.by import By
    
    options = Options()
    # options.add_experimental_option("detach", True)
    
    driver = webdriver.Chrome(
        service=Service(ChromeDriverManager().install()),
         options=options
         )
    
    driver.get("https://www.python.org/")
    driver.maximize_window()
    
    input = driver.find_element(By.CSS_SELECTOR,".documentation-widget p")
    
    print(input)
    
    driver.close()
    driver.quit()
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 2019-04-30
      • 2015-09-13
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      相关资源
      最近更新 更多