我最近能够得到一个无头的webdriver,并且已经修补了一些。目前,我无法通过xpath定位元素并更改其文本。下面,我试图将“ENTER TEXT HERE”返回到控制台,然后将文本更改为“hello world”,然后显示新更改的元素文本。
HTML:
<div id="textfield" class="newInput">
<div>ENTER TEXT HERE</div>
</div>
选择xpath后,我得到了这个:
//*[@id='textfield']/div
这一切都很好,但我很困惑如何改变文本。 试图在我尝试的变量中存储“ENTER TEXT HERE”:
的Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
defaultText = browser.find_element_by_xpath("//*[@id='textfield']/div").getText()
print defaultText
browser.find_element_by_xpath("//*[@id='textfield']/div").textContent = "hello world"
defaultText = browser.find_element_by_xpath("//*[@id='textfield']/div").getText()
print defaultText
#Attempt to store "ENTER TEXT HERE" into variable
#Attempt to print ENTER TEXT HERE in console
#Change text to "hello world"
#Update string in variable
#Attempt to print hello world in console
非常感谢任何帮助或见解。谢谢!
编辑:
感谢Richard指出send_keys(), 我现在处于一种困境,如果默认情况下没有文本并且您尝试发送字符串,则会抛出异常。跟踪会导致webdriver错误处理程序中的.check_response。
Traceback (most recent call last):
File "test.py", line 116, in <module>
browser.find_element_by_xpath("//*[@id='textfield']/div").send_keys("hello world")
File "C:\Python26\lib\site-packages\selenium\webdriver\remote\webelement.py", line 293, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "C:\Python26\lib\site-packages\selenium\webdriver\remote\webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "C:\Python26\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "C:\Python26\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'{"errorMessage":"\'undefined\'
is not an object (evaluating \'b.value.length\')","request":{"headers":
{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-
Length":"147","Content-Type":"application/json;charset=UTF8","Host":"127.0.0.1:XXXXX",
"User-Agent":"Python-urllib/2.6"},"httpVersion":"1.1","
method":"POST","post":"{\\"sessionId\\": \\"561558d0-36a9-11e4-c9e7-c90efed32dfc\\",
\\"id\\": \\":wdc:1410101175394\\", \\"value\
\": [\\"h\\", \\"e\\", \\"l\\", \\"l\\", \\"o\\", \\" \\", \\"w\\", \\"o\\", \\"r\\",
\\"l\\", \\"d\\"]}","url":"/value","urlParsed":
{"anchor":"","query":"","file":"value","directory":"/","path":"/value","relative":"/value",
"port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"",
"source":"/value","queryKey":{},"chunks":["value"]},"urlOriginal":"/session/561
558d0-36a9-11e4-c9e7-c90efed32dfc/element/%3Awdc%3A1410101175394/value"}}' ;
Screenshot: available via screen
答案 0 :(得分:0)
您无法将文本设置为值,您需要使用send_keys()
来设置元素的文本。另外,getText()
是从元素获取文本的java方法,您需要使用text
和python。
defaultText = browser.find_element_by_xpath("//*[@id='textfield']/div").text
print defaultText
browser.find_element_by_xpath("//*[@id='textfield']/div").send_keys("hello world")
defaultText = browser.find_element_by_xpath("//*[@id='textfield']/div").text
print defaultText