Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium: Find object attributes using xpath

Tags:

I am new to xpath, trying to get value of the "value" using xpath:

<input type="submit" value="  Search  " class="long searchButton" style="width:190px !important;"> 

while it is easy to find element by "type="submit" like:

browser.find_elements_by_xpath("//*[@type='submit']") 

I haven't been able to figure out how to get the values I need, as:

browser.find_elements_by_xpath("//*[@type='submit']/@value") 

somewhat expectedly gives an error:

expression "//*[@type=\'submit\']/@value" is: [object Attr]. It should be an element 

Any ideas how to solve this?

EDIT: The xpath is correct, but it "returns" an obj attribute and as it is not an element, it is not allowed. I can't find a method like get_attr_by_xpath(), or anything similar.

like image 995
root Avatar asked Sep 25 '12 08:09

root


People also ask

How do I use XPath to find elements?

To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.

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.

How does XPath find elements in Selenium?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

What is find element by XPath Selenium Python?

You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.


1 Answers

I finally used get_attribute("value") as:

for i in browser.find_elements_by_xpath("//*[@type='submit']"):     print i.get_attribute("value") 
like image 118
root Avatar answered Sep 29 '22 18:09

root