Is there a way to find an element by attributes in Python Selenium?

后端 未结 3 964
南旧
南旧 2020-11-30 09:42

I got a html snippet like this:



        
相关标签:
3条回答
  • 2020-11-30 10:06

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

    driver.find_element_by_xpath('//input[@node-type="searchInput"]')
    
    0 讨论(0)
  • 2020-11-30 10:16

    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"]')

    0 讨论(0)
  • 2020-11-30 10:32

    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)
    
    
    0 讨论(0)
提交回复
热议问题