简体   繁体   English

通过特定的类名选择元素

[英]Selecting elements by specific Class Name

I would like to select elements by the class name, but only with a specific name (first-level-li) , if there are other classes with the name that I am trying to select but followed for other names我想按类名选择元素,但只有一个特定的名称(first-level-li) ,如果有其他类的名称我试图选择但其他名称

(<li class="first-level-li no-second-level shop-link">
<li class="first-level-li  no-second-level  ">)

, I need to ignore them, how can I do this? ,我需要忽略它们,我该怎么做? Example:例子:

firstLevelMenu = self.driver.find_elements_by_class_name ( "first-level-li" )

website网站

<li class="first-level-li no-second-level shop-link">
<li class="first-level-li  no-second-level  ">
<li class="first-level-li  ">
firstlevelMenu = self.driver.find_elements_by_xpath("//li[@class='first-level-li  ']")

文档在这里http://selenium-python.readthedocs.io/locating-elements.html

You can use xpath to do the same:您可以使用 xpath 来做同样的事情:

self.driver.find_elements_by_xpath("//li[@class='first-level-li']")

This finds the element that has the exact class name.这将查找具有确切类名的元素。

You can use xpath as mentioned above with following function too which will directly return object of specific element, instead of list.您也可以使用上面提到的 xpath 和以下函数,它将直接返回特定元素的对象,而不是列表。

self.driver.find_element_by_xpath("//li[@class='first-level-li ']") self.driver.find_element_by_xpath("//li[@class='first-level-li']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM