Selenium Webdriver Python如何从输入标记

时间:2015-09-17 13:02:34

标签: selenium xpath selenium-webdriver

我正在尝试获取文本字段的值。文本字段位于html的Input标记中。我想打印出它的价值。 我没有打印到控制台。 我已使用以下XPATH

标识了该元素
By.XPATH, '//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "Type")]/following-sibling::*'

我的代码段是

 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

HTML是:

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

我也尝试过以下方法:

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::*'))

我收到JavaScript错误:

raise exception_class(message, screen, stacktrace)
WebDriverException: Message: JavaScript error

我如何获得Textfield的值?

谢谢,

1 个答案:

答案 0 :(得分:3)

由于value只是一个html属性,因此您可以使用 -

获取属性值
print type_field_element.get_attribute('value')
相关问题