gpt4 book ai didi

python - 为什么 Selenium 找不到标签和类

转载 作者:行者123 更新时间:2023-12-04 08:52:47 25 4
gpt4 key购买 nike

我最近开始使用 Selenium ,但我无法解决一个问题。当我使用 find_element_by_tag(class)_name 或 _css_selector 时引发错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
但我正在寻找的元素存在。
例如:
from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

browser = webdriver.Chrome(ChromeDriverManager().install())
url='http://inventwithpython.com/'
browser.get(url)
a = browser.find_element_by_class_name('navbar navbar-toggleable-md')
输出:
[WDM] - Current google-chrome version is 85.0.4183

[WDM] - Get LATEST driver version for 85.0.4183
[WDM] - Driver [C:\Users\vladv\.wdm\drivers\chromedriver\win32\85.0.4183.87\chromedriver.exe] found in cache
Traceback (most recent call last):
File "C:/Users/vladv/PycharmProjects/untitled1/черновик.py", line 14, in <module>
a = browser.find_element_by_class_name('navbar navbar-toggleable-md')
File "C:\Users\vladv\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\vladv\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\vladv\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\vladv\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".navbar navbar-toggleable-md"}
(Session info: chrome=85.0.4183.102)
但是如果我想找到“navbar-brand”类,我可以做到。
有什么问题?
如果我使用 find_elements,则返回空列表。
P.S 如果有人可以建议一些有用的 Material 来研究 Selenium ,那将非常有帮助。

最佳答案

根据 selenium.webdriver.common.by 的文档执行:

class selenium.webdriver.common.by.By
Set of supported locator strategies.

CLASS_NAME = 'class name'

因此,使用 find_element_by_class_name()您将无法传递多个类名。
传递多个类,您将面临错误。

解决方案
要定位元素,您可以使用以下任一方法 Locator Strategies :
  • 使用 css_selector :
    element = driver.find_element_by_css_selector("nav.navbar.navbar-toggleable-md")
  • 使用 xpath :
    element = driver.find_element_by_xpath("//nav[@class='navbar navbar-toggleable-md']")

  • 理想情况下,要定位需要诱导的元素 WebDriverWaitvisibility_of_element_located()您可以使用以下任一 Locator Strategies :
  • 使用 CSS_SELECTOR :
    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "nav.navbar.navbar-toggleable-md")))
  • 使用 XPATH :
    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//nav[@class='navbar navbar-toggleable-md']")))
  • 备注 :您必须添加以下导入:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

  • 引用
    您可以在以下位置找到一些相关的详细讨论:
  • Invalid selector: Compound class names not permitted error using Selenium
  • 关于python - 为什么 Selenium 找不到标签和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64017325/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com