Python Selenium 打印出文本字段的值显示为空。该值未打印

标签 python selenium-webdriver webdriver

我正在尝试将文本字段的值打印到控制台。 该网页在文本字段中的值为 1,000.000。 1,000.000 应该打印,但我的方法是打印空白。
我正在使用 Python Webdriver。我正在使用 .text,它应该获取文本字段的文本值。

我的方法是:

from selenium.webdriver.common.by import By

# max records textfield has the value 1,000.000 as default
def print_maxrecords_textfield(self):
    max_records_textfield = self.driver.find_element((By.XPATH, '//span[@class="gwt-InlineLabel myinlineblock marginbelow" and contains(text(), "Max records")]/following-sibling::*'))
    print "max_records_textfield = "
    print max_records_textfield.text

我将测试用例类中的方法调用为 dp.print_maxrecords_textfield()

控制台输出如下:

max_records_textfield = 

它应该说 max_records_textfield = 1,000.00

HTML 片段是:

<div class="padding gwt-TabLayoutPanelContent" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;" aria-hidden="false">
    <div class="clear">
        <span class="gwt-InlineLabel marginbelow myinlineblock" style="width: 8em;">Location</span>
        <input class="gwt-TextBox marginbelow" type="text" style="width: 30em;"/>
    </div>
    <div class="clear">
        <span class="gwt-InlineLabel myinlineblock marginbelow" style="width: 8em;">Max records</span>
        <input class="gwt-IntegerBox marginbelow" type="text"/>
    </div>

最佳答案

实际尝试获取值而不是文本。

from selenium.webdriver.common.by import By

# max records textfield has the value 1,000.000 as default
def print_maxrecords_textfield(self):
    max_records_textfield = self.driver.find_element((By.XPATH, '//span[@class="gwt-InlineLabel myinlineblock marginbelow" and contains(text(), "Max records")]/following-sibling::*'))
    print "max_records_textfield = "
    print max_records_textfield.get_attribute('value')

关于Python Selenium 打印出文本字段的值显示为空。该值未打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31609234/

相关文章:

python - Where 函数忽略 Nan

python - 为什么这个问题发生在Python函数作用域中?

java - 如何在 testng 的单个套件中多次运行测试类

c# - 我如何找到元素颜色;切换时我需要检查的元素会更改颜色

selenium - 如何使用 webdriver 在 IE 中单击选择选项?

python - 在 python 中使用 subprocess.run 以管理员身份运行进程

Python:为什么这个打印会自动刷新到屏幕而不需要flush()?

java - 使用 Selenium Webdriver 时如何打开通常的 chrome 或 Firefox 窗口?

c# - c# 中的 Selenium WaitForElement 抛出 ElementNotVisibleException

python - 如何使用 webdriver 获取文本区域的文本内容?