Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (29.3k points)

I must be thinking about this wrong.

I want to get the contents of an element, in this case, a formfield, on a page that I am accessing with Webdriver/Selenium 2

Here is my broken code:

 Element=driver.find_element_by_id(ElementID)

 print Element

 print Element.text

here is the result:

<selenium.webdriver.remote.webelement.WebElement object at 0x9c2392c>

(Notice the blank line) I know that element has contents since I just stuffed them in there with the previous command using .sendkeys and I can see them on the actual web page while the script runs.

but I need to get the contents back into data.

What can I do to read this? Preferably in a generic fashion so that I can pull contents from varied types of elements.

1 Answer

0 votes
by (50.2k points)

 You would need to use element.get_attribute('value') for input elements and element.text to return the text node of an element.

You can check the WebElement object with element.tag_name to find out what kind of element it is. 

For example:-

driver = webdriver.Firefox()

driver.get('http://www.w3c.org')

element = driver.find_element_by_name('q')

element.send_keys('hi mom')

element_text = element.text

element_attribute_value = element.get_attribute('value')

print element

print 'element.text: {0}'.format(element_text)

print 'element.get_attribute(\'value\'): {0}'.format(element_attribute_value)

driver.quit()

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...