有没有办法在Python Selenium中按属性查找元素?

arm*_*ong 16 python selenium

我有一个像这样的html片段:

<input type="text" node-type="searchInput" autocomplete="off" value="" class="W_input" name="14235541231062">
Run Code Online (Sandbox Code Playgroud)

html中这个元素的唯一唯一标识是属性node-type="searchInput",所以我想通过使用Python selenium的一些方法来定位它,如下所示:

search_elem = driver.find_element_by_xxx("node-type","searchInput") # maybe?
Run Code Online (Sandbox Code Playgroud)

我已经检查了selenium(python)文件以找到elems 但是没有得到如何通过node-typeattr 找到这个元素的线索.有没有明确的方法在python selenium中找到这个元素?

ale*_*cxe 26

您可以通过xpath获取它并检查node-type属性值:

driver.find_element_by_xpath('//input[@node-type="searchInput"]')
Run Code Online (Sandbox Code Playgroud)

  • 属性的CSS选择符表示法是:tag_name [attribute_name = attribute_value]。比XPath更简单,更快 (3认同)

小智 9

即使这个问题很老,但我相信它仍然非常相关。您可以使用简单的 css 选择器,其语法是标准的 javascript,类似于 jquery 或本机浏览器支持。

driver.find_element_by_css_selector('span.className[attrName="attrValue"]')

例子: driver.find_element_by_css_selector('span.blueColor[shape="circle"]')

  • 对于不属于某个类的标签,请从搜索中删除 className 属性:`driver.find_element_by_css_selector('[shape="circle"]')` (2认同)