How can I get the current contents of an element in webdriver

How can I get the current contents of an element in webdriver



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.






what kind of element is it? If it's an input element you need the attribute "value" instead of text

– prestomanifesto
Apr 20 '12 at 23:02






Ah, Value could be the solution. Will be trying.

– Skip Huffman
Apr 23 '12 at 12:55




5 Answers
5



I believe prestomanifesto was on the right track. It depends on what kind of element it is. You would need to use element.get_attribute('value') for input elements and element.text to return the text node of an element.


element.get_attribute('value')


element.text



You could check the WebElement object with element.tag_name to find out what kind of element it is and return the appropriate value.


element.tag_name



This should help you figure out:


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()






Thanks.. I have been crawling the website for this answer for hours!!

– Kehlin Swain
May 29 '15 at 7:31






@KehlinSwain Happy to help :)

– Isaac
May 29 '15 at 17:13






Does this same method works if there are multiple classes in a row, and you want to get the text of each class in that row?

– Kehlin Swain
May 29 '15 at 21:23


element.get_attribute('innerHTML')






really useful command to get the attributes that do not actually appear on the page such as hrefs etc. plus 1

– nerak99
Jun 11 '18 at 15:54



My answer is based on this answer: How can I get the current contents of an element in webdriver
just more like copy-paste.


from selenium import webdriver

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))


element = driver.find_element_by_css_selector('.description.expand_description > p')
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()



I know when you said "contents" you didn't mean this, but if you want to find all the values of all the attributes of a webelement this is a pretty nifty way to do that with javascript in python:


everything = b.execute_script(
'var element = arguments[0];'
'var attributes = ;'
'for (index = 0; index < element.attributes.length; ++index) '
' attributes[element.attributes[index].name] = element.attributes[index].value ;'
'var properties = ;'
'properties[0] = attributes;'
'var element_text = element.textContent;'
'properties[1] = element_text;'
'var styles = getComputedStyle(element);'
'var computed_styles = ;'
'for (index = 0; index < styles.length; ++index) '
' var value_ = styles.getPropertyValue(styles[index]);'
' computed_styles[styles[index]] = value_ ;'
'properties[2] = computed_styles;'
'return properties;', element)



you can also get some extra data with element.__dict__.


element.__dict__



I think this is about all the data you'd ever want to get from a webelement.



In Java its Webelement.getText() . Not sure about python.






The question tag implies that it is about python

– illright
May 25 '16 at 15:55


python



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế