Selenium Webdriver Python 如何从输入标签中获取文本 [英] Selenium Webdriver Python How to get text from the input tag

查看:61
本文介绍了Selenium Webdriver Python 如何从输入标签中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取文本字段的值.文本字段位于 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::*'')打印 type_field_element.text

HTML 是:

<span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">Type</span><input class="gwt-TextBox marginbelow" type="text" disabled="" style="display: inline;"/>

我也试过以下方法:

def get_type_field_value2(self):返回 self.driver.execute_script("""返回 jQuery(arguments[0]).contents().filter(function() {返回 this.nodeType == Node.TEXT_NODE;}).文本();""", 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:消息:JavaScript 错误

如何获取文本字段的值?

谢谢,

解决方案

由于 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')

这篇关于Selenium Webdriver Python 如何从输入标签中获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆