Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 414
armnotstrong Avatar asked Feb 10 '15 07:02

armnotstrong


People also ask

How can the user find the element by attribute in Selenium?

We can find an element using the attribute name with Selenium webdriver using the locators - name, css, or xpath. To identify the element with css, the expression should be tagname[name='value'] and the method to be used is By. cssSelector.

How do you find the attributes of an element in Python?

get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag. This method will first try to return the value of a property with the given name. If a property with that name doesn't exist, it returns the value of the attribute with the same name.

Can I find element by class Selenium Python?

Luckily, Selenium offers a few methods you can use to find elements. One of the options the Selenium WebDriver library provides for locating HTML elements is the use of the class property. The HTML class property is used for setting a class to an HTML element.


2 Answers

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

driver.find_element_by_xpath('//input[@node-type="searchInput"]') 
like image 184
alecxe Avatar answered Oct 13 '22 18:10

alecxe


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

like image 33
itwasnoteasy Avatar answered Oct 13 '22 17:10

itwasnoteasy