gpt4 book ai didi

Selenium Webdriver Python 如何从输入标签获取文本

转载 作者:行者123 更新时间:2023-12-02 14:47:41 25 4
gpt4 key购买 nike

我正在尝试获取文本字段的值。文本字段位于 html 中的输入标记中。我想打印出它的值。我没有在控制台上打印任何内容。我已使用以下 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

如何获取文本字段的值?

谢谢

最佳答案

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

print type_field_element.get_attribute('value')

关于Selenium Webdriver Python 如何从输入标签获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32630986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com