4

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?

2 Answers 2

9

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");
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.