52

I got a html snippet like this:

<input type="text" node-type="searchInput" autocomplete="off" value="" class="W_input" name="14235541231062">

The only unique identity of this element in the html is the attribute node-type="searchInput", so I want to locate it by using some method of Python selenium sort of like this:

search_elem = driver.find_element_by_xxx("node-type","searchInput") # maybe?

I have checked the selenium (python) document for locating elems but didn't get a clue of how to locate this elem by the node-type attr. Is there a explicit way to locate this elem in python selenium?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • 2
    Possible duplicate of [Find element by attribute](https://stackoverflow.com/questions/26304224/find-element-by-attribute) – GIA Jul 02 '18 at 22:21

3 Answers3

71

You can get it by xpath and check the node-type attribute value:

driver.find_element_by_xpath('//input[@node-type="searchInput"]')
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
15

Even though the question is old but it's still very relevant I believe. You may be able to use simple css selector and the syntax is standard javascript similar to jquery or native browser support.

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

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

itwasnoteasy
  • 461
  • 4
  • 7
3

Here's a method you could use:

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)
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69