我正在尝试单击类名称等于 "clean right"
的以下元素:
<li class="clean right"></li>
如何使用 driver.find_element_by_class_name()
找到它
最佳答案
您不能通过 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")
如果
classnames
、clean
和right
都是强制标识元素,可以使用css-selectors如下:driver.find_element_by_css_selector("li.clean.right")
您也可以使用 xpath 作为替代方案。如下:
driver.find_element_by_xpath("//li[@class='clean right']")
;博士
Invalid selector: Compound class names not permitted error using Selenium
引用
关于python - 如何使用 Selenium 和 Python 定位具有多个类名的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60534244/