How to locate an element with multiple class names?

南楼画角 提交于 2021-01-19 12:45:47

问题


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

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

How could I locate it by using driver.find_element_by_class_name()


回答1:


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?




回答2:


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


来源:https://stackoverflow.com/questions/60534244/how-to-locate-an-element-with-multiple-class-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!