如何在网络驱动程序中获取元素的当前内容

我一定是想错了。

我希望在我使用 WebDriver/Selenium 2访问的页面上获得一个元素的内容,在本例中是一个 formfield

这是我的破译代码:

 Element=driver.find_element_by_id(ElementID)
print Element
print Element.text

结果是这样的:

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

(注意空行) 我知道这个元素有内容,因为我刚刚使用。Sendkey 和我可以看到他们在实际的网页,而脚本运行。

但我需要把内容重新输入数据。

我要怎么做才能读到这个?最好是以通用的方式,这样我就可以从不同类型的元素中提取内容。

187928 次浏览

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

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.

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

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()
element.get_attribute('innerHTML')

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__.

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