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.
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.
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.
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.
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.
I finally used get_attribute("value")
as:
for i in browser.find_elements_by_xpath("//*[@type='submit']"): print i.get_attribute("value")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With