如何使用webdriver(python)检索在文本字段中输入的文本?

2024-03-27 18:02:57 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个非常基本的问题,但我在任何地方都找不到答案。。。如何使用python下的webdriver检索在文本字段中输入的文本?我试过几个texttext()都没用。

>>> driver.get("http://en.wikipedia.org/wiki/Main_Page")
>>> el = driver.find_elements_by_xpath("//input[contains(@name, 'search')]")
>>> el
[<selenium.webdriver.remote.webelement.WebElement object at 0x10d15f6d0>]
>>> el[0].send_keys("orange") # this works
>>> el[0].text
''
>>> el[0].text()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> el[0].get_text()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'WebElement' object has no attribute 'get_text'

谢谢!


Tags: text文本mostgetobjectdriverstdincall
2条回答

当我使用firebug调查Wikipedia站点上的search元素时,在我看来,输入的文本值存储在value属性中。所以你必须得到这个属性。

因为我一直只使用属性操作。对我来说很好。使用下面的代码。哪些文本需要从输入中获取

Description=driver.find_element_by_xpath("//*[@id='id_description']")
Description.clear()
Description.send_keys("xxx")
Description.get_attribute("xxxx")
print Description

相关问题 更多 >