2012-02-18 71 views
3

這是一個非常基本的問題,但我無法在任何地方找到答案...如何檢索在python下使用webdriver在文本字段中輸入的文本?我試過幾個texttext()都不行。如何檢索使用webdriver(python)在文本字段中輸入的文本?

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

謝謝!

+0

我不知道這是否工作(因爲我無法測試它),但也許嘗試獲取值屬性的內容。在我看來,輸入的文本是在值屬性中。 – tester 2012-02-19 13:02:58

+2

ding ding'el [0] .get_attribute(「value」)'做到了,謝謝!如果您在答覆中回覆,我可以用複選標記接受... – 2012-02-20 00:39:43

回答

4

當我調查維基百科網站上的螢火蟲搜尋元素時,我覺得輸入的文字值是儲存在值屬性中。所以你必須得到這個屬性。

11

由於我一直只使用屬性動作。它對我來說工作得很好。請使用下面的代碼。哪些文本需要從輸入得到

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