Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium - print xpath value

Here is my code:

print browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small')

It prints <selenium.webdriver.remote.webelement.WebElement object at 0x02915310> but I want it to print the actual value, which would be 0.00

Any ideas?

like image 517
user3196332 Avatar asked Oct 25 '25 21:10

user3196332


2 Answers

This should work for you for inner text:

print browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small').text

Alternatively, if you're trying to get the value:

print browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small').get_attribute("value");
like image 169
Richard Avatar answered Oct 28 '25 12:10

Richard


When you find an element you don't find the text of the element, but actually much more. It is a WebElement python object, which has a bunch of useful actions and values associated with it.

So the text of a the element is found by calling the text property is what you a currently looking for

browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small')

we get the WebElement with browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small')

but for the text we use browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small').text

and i am guessing you are going to want work with that as a float value, so ten its float(browser.find_element_by_xpath('/html/body/div[2]/div/div/div/h1/div/small').text)

But there is much more you can look in a WebElement for other WebElements, for like

foo = browser.find_element_by_xpath('/html/body/p') 
bar = foo.find_element_by_xpath('/input')
baz = foo.find_element_by_xpath('button')

you can do things like bar.get_attribute('name') which will return a string or bar.send_keys('good stuff') or baz.click()

The documents can be found here

http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.remote.webelement

like image 24
Victory Avatar answered Oct 28 '25 14:10

Victory