更新时间:2023-02-19 17:16:47
由于 value 只是一个 html 属性,您可以使用 -
获取属性值print type_field_element.get_attribute('value')
I am trying to get the value of a textfield. The textfield is in an Input tag in the html. I want to print out it's value. I am getting nothing printed out to the console. I have identified the element with the following XPATH
By.XPATH, '//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "Type")]/following-sibling::*'
My code snippet is
def get_type_field_value(self):
type_field_element = self.driver.find_element(By.XPATH, 'By.XPATH, '//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "Type")]/following-sibling::*'')
print type_field_element.text
The HTML is:
<div class="clear">
<span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">Type</span>
<input class="gwt-TextBox marginbelow" type="text" disabled="" style="display: inline;"/>
</div>
I have also tried the following method:
def get_type_field_value2(self):
return self.driver.execute_script("""
return jQuery(arguments[0]).contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text();
""", self.driver.find_element(By.XPATH, '//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "Type")]/following-sibling::*'))
I get a JavaScript error:
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: JavaScript error
How do i get the value of the Textfield?
Thanks,
Since value is simply an html attribute, you can get the attribute value using -
print type_field_element.get_attribute('value')