本文介绍了如何使用 Selenium/Webdriver 提示输入并使用结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想允许用户输入并根据它做出一些决定.如果我这样做:
I would like to allow for user input and make some decisions based on it. If I do this:
driver.execute_script("prompt('Enter smth','smth')")
我得到了一个很好的提示,但我不能使用它的值.有没有办法向用户显示输入框,并使用输入的值?
I get a nice prompt, but I cannot use it's value. Is there any way of showing an input box to the user, and use the value typed there?
这是我的脚本:
from selenium.webdriver import Firefox
if __name__ == "__main__":
driver = Firefox()
driver.execute_script("window.promptResponse=prompt('Enter smth','smth')")
a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")
print "got back %s" % a
这会以以下异常退出:
a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")
File "c:\python26\lib\site-packages\selenium-2.12.1-py2.6.egg\selenium\webdriver\remote\webdriver.py", line 385, in ex
ecute_script
{'script': script, 'args':converted_args})['value']
File "c:\python26\lib\site-packages\selenium-2.12.1-py2.6.egg\selenium\webdriver\remote\webdriver.py", line 153, in ex
ecute
self.error_handler.check_response(response)
File "c:\python26\lib\site-packages\selenium-2.12.1-py2.6.egg\selenium\webdriver\remote\errorhandler.py", line 110, in
check_response
if 'message' in value:
TypeError: argument of type 'NoneType' is not iterable
我做错了什么?
我试着像 prestomanifesto 建议的那样做,这是输出:
I tried to do like prestomanifesto suggested, here's the output:
In [1]: from selenium.webdriver import Firefox
In [2]: f = Firefox()
In [3]: a = f.ex
f.execute f.execute_async_script f.execute_script
In [3]: a = f.execute_script("return prompt('Enter smth','smth')")
In [4]: a
Out[4]: {u'text': u'Enter smth'}
In [5]: a
Out[5]: {u'text': u'Enter smth'}
In [6]: class(a)
File "<ipython-input-6-2d2ff4f61612>", line 1
class(a)
^
SyntaxError: invalid syntax
In [7]: type(a)
Out[7]: dict
推荐答案
希望这对其他人有帮助:
Hope this helps others:
# selenium (3.4.1) python (3.5.1)
driver.execute_script("var a = prompt('Enter Luffy', 'Luffy');document.body.setAttribute('data-id', a)")
time.sleep(3) # must
print(self.driver.find_element_by_tag_name('body').get_attribute('data-id')) # get the text
这篇关于如何使用 Selenium/Webdriver 提示输入并使用结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!