Python Selenium打印文本框的值为空。该值未被打印出来。

3

我正在尝试将文本框的值打印到控制台。

网页上的文本框中有值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>
1个回答

3

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

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

1
这个可以,我得到了输出max_records_textfield = 1,000,000,非常感谢。那么.text是什么意思呢? - Riaz Ladhani
get_attribute('value')捕获存储实际值的属性。 另一方面,text捕获innertext。 - Saifur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接