【问题标题】:How to locate an element with multiple classnames using Selenium and Python如何使用 Selenium 和 Python 定位具有多个类名的元素
【发布时间】:2020-06-17 10:17:32
【问题描述】:

我正在尝试单击类名等于"clean right" 的以下元素:

<li class="clean right"></li>

如何使用driver.find_element_by_class_name()找到它

【问题讨论】:

标签: python selenium xpath css-selectors classname


【解决方案1】:

您不能通过find_element_by_class_name() 传递多个类名作为参数,这样做您将面临如下错误:

invalid selector: Compound class names not permitted

有多种方法可以解决此用例,您可以使用以下任一Locator Strategies

  • 如果元素仅通过classname 唯一标识clean,您可以使用:

    driver.find_element_by_class_name("clean")
    
  • 如果元素仅通过classname 唯一标识right,您可以使用:

    driver.find_element_by_class_name("right")
    
  • 如果classnamescleanright都是强制标识元素,您可以使用,如下所示:

    driver.find_element_by_css_selector("li.clean.right")
    
  • 您也可以使用,如下所示:

    driver.find_element_by_xpath("//li[@class='clean right']")
    

tl;博士

Invalid selector: Compound class names not permitted error using Selenium


参考

Find div element by multiple class names?

【讨论】:

    【解决方案2】:

    前面的答案部分不正确。请查看源代码:

    https://github.com/SeleniumHQ/selenium/blob/9160de55af9cc230f758f4ce6a2af8d1570f0614/py/selenium/webdriver/remote/webdriver.py

    你可以对多个类使用class_name,只需要用'.'替换空格

    使用带空格的类的示例:

    from selenium import webdriver
    from time import sleep
    
    options = webdriver.ChromeOptions()
    #options.headless = True
    options.add_argument("--window-size=1920,1080")
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument(
        "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
    browser = webdriver.Chrome(options=options)
    browser.get("https://www.instagram.com")
    sleep(5)
    #browser.refresh()
    elem=browser.find_element_by_class_name('RP4i1.UVauz')
    print(elem.get_attribute("outerHTML"))
    browser.get_screenshot_as_file(f"screenshot.png")
    

    输出:

    <img class="RP4i1  UVauz" src="/static/images/homepage/screenshot1.jpg/d6bf0c928b5a.jpg" alt="">
    

    如果您从 by_class_name 中检查异常:

    您可以看到它在后台使用 css_class 定位器(您可以看到它在前面自动添加 .)

    另一个工作示例:

    from selenium import webdriver
    
    import time
    
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.get("https://stackoverflow.com/questions/65579491/find-element-by-class-name-in-selenium-giving-error/65579606?noredirect=1#comment115946541_65579606")
    time.sleep(5)
    elem = driver.find_element_by_class_name('overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100')
    
    print(elem.get_attribute("outerHTML"))
    

    【讨论】:

    • 我不得不否决这个答案,因为这个答案在概念上是错误的。你完全搞乱了定位器的概念。
    • @DebanjanB 你能解释一下为什么吗?
    • @DebanjanB 你认为答案在哪个级别是错误的?
    • 答案也提供了一个工作示例
    • by_class_name 只是添加了 '.'在提供的定位器前面,因此“a”将作为 .a 传递,a.b 将作为“.a.b”传递
    猜你喜欢
    • 2015-10-04
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多