How to locate an element with multiple class names?

后端 未结 2 629
感动是毒
感动是毒 2021-01-17 06:35

I\'m trying to click on the following element with the class name equals \"clean right\":

  • 相关标签:
    2条回答
    • 2021-01-17 06:59

      The previous answer is partially incorrect. please check the source code at :

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

      You can use class_name for multiple classes , just need to replace space with '.'

      Example for using class with space:

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

      Output:

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

      If you check the exception from the by_class_name:

      You can see that it is using css_class locator under the hood ( You can see it add . in frontautomatically)

      Another Working example:

      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"))
      
      0 讨论(0)
    • 2021-01-17 07:00

      You can't pass multiple classnames as argument through find_element_by_class_name() and doing so you will face an error as:

      invalid selector: Compound class names not permitted
      

      There are multiple approaches to solve this usecase and you can use either of the following Locator Strategies:

      • If the element is uniquely identified only through the classname clean you can use:

        driver.find_element_by_class_name("clean")
        
      • If the element is uniquely identified only through the classname right you can use:

        driver.find_element_by_class_name("right")
        
      • If both the classnames, clean and right are mandatory to identify the element, you can use css-selectors as follows:

        driver.find_element_by_css_selector("li.clean.right")
        
      • As an alternative you can also use xpath as follows:

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

      tl; dr

      Invalid selector: Compound class names not permitted error using Selenium


      Reference

      Find div element by multiple class names?

      0 讨论(0)
    提交回复
    热议问题