我想允許用戶輸入並根據它做出一些決定。如果我這樣做:如何使用Selenium/Webdriver提示輸入並使用結果?
driver.execute_script("prompt('Enter smth','smth')")
我得到一個不錯的提示,但我不能用它的價值。是否有任何方法向用戶顯示輸入框,並使用在那裏鍵入的值?
編輯:這是我的腳本:
從selenium.webdriver導入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建議,這裏的輸出:
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
我更新了我與運行代碼的結果問題。你能發現我做得不對嗎? – Geo