有没有办法在 Python Selenium 中通过属性查找元素?
Is there a way to find an element by attributes in Python Selenium?
我得到了这样的 html 片段:
<input type="text" node-type="searchInput" autocomplete="off" value="" class="W_input" name="14235541231062">
这个元素在html中的唯一唯一标识是属性node-type="searchInput"
,所以我想通过一些方法来定位它Python selenium 有点像这样:
search_elem = driver.find_element_by_xxx("node-type","searchInput") # maybe?
我已经检查了 selenium(python) document for locating elems 但不知道如何通过 node-type
属性找到这个元素。有没有明确的方法在 python selenium 中定位这个元素?
可以通过xpath获取,查看node-type
属性值:
driver.find_element_by_xpath('//input[@node-type="searchInput"]')
您可以使用以下方法:
save_items = []
for item in driver.find_elements_by_tag_name("input"):
# Get class
item_class = item.get_attribute("class")
# Get name:
item_name = item.get_attribute("name")
# And so on...
# Check for a match
if item_class == "W_input" and item_name == "14235541231062":
# Do your operation (or add to a list)
save_items.append(item)
尽管这个问题很老,但我相信它仍然非常相关。
您可以使用简单的 css 选择器,语法是标准 javascript 类似于 jquery 或本机浏览器支持。
driver.find_element_by_css_selector('span.className[attrName="attrValue"]')
示例:
driver.find_element_by_css_selector('span.blueColor[shape="circle"]')
我得到了这样的 html 片段:
<input type="text" node-type="searchInput" autocomplete="off" value="" class="W_input" name="14235541231062">
这个元素在html中的唯一唯一标识是属性node-type="searchInput"
,所以我想通过一些方法来定位它Python selenium 有点像这样:
search_elem = driver.find_element_by_xxx("node-type","searchInput") # maybe?
我已经检查了 selenium(python) document for locating elems 但不知道如何通过 node-type
属性找到这个元素。有没有明确的方法在 python selenium 中定位这个元素?
可以通过xpath获取,查看node-type
属性值:
driver.find_element_by_xpath('//input[@node-type="searchInput"]')
您可以使用以下方法:
save_items = []
for item in driver.find_elements_by_tag_name("input"):
# Get class
item_class = item.get_attribute("class")
# Get name:
item_name = item.get_attribute("name")
# And so on...
# Check for a match
if item_class == "W_input" and item_name == "14235541231062":
# Do your operation (or add to a list)
save_items.append(item)
尽管这个问题很老,但我相信它仍然非常相关。 您可以使用简单的 css 选择器,语法是标准 javascript 类似于 jquery 或本机浏览器支持。
driver.find_element_by_css_selector('span.className[attrName="attrValue"]')
示例:
driver.find_element_by_css_selector('span.blueColor[shape="circle"]')