Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prompt for input using Selenium/Webdriver and use the result?

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?

EDIT: This is my script:

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

And this exits with the following exception:

    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

What am I not doing right?

EDIT: 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
like image 425
Geo Avatar asked Nov 17 '11 22:11

Geo


3 Answers

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
like image 174
Devin Avatar answered Nov 09 '22 18:11

Devin


I know this is an old question but I had the same question too and this is what works for me: Some credit to @Devin

from selenium.common.exceptions import UnexpectedAlertPresentException

while True:
    try:
        driver.execute_script("var a = prompt('Enter Luffy', 'Luffy');document.body.setAttribute('user-manual-input', a)")
        sleep(5)  # must 
        print(driver.find_element_by_tag_name('body').get_attribute('user-manual-input')) # get the text
        break

     except (UnexpectedAlertPresentException):
        pass

The prompt will wait 5 seconds for input. If no input is provided, it will prompt the user for input again.

like image 33
Anatol Avatar answered Nov 09 '22 20:11

Anatol


You are correct in using the prompt box in javascript. But the prompt box value should be assigned to a global variable and then you could use this variable later. something like this:

driver.execute_script("window.promptResponse=prompt('Enter smth','smth')")

and then retrieve the value from the same global variable.

a = driver.execute_script("var win = this.browserbot.getUserWindow(); return win.promptResponse")

you probably need to cast the return.

Hope this helps.

like image 4
Baz1nga Avatar answered Nov 09 '22 20:11

Baz1nga