How can I request data entry using Selenium / Webdriver and use the result?

I would like to allow user input and make some decisions based on it. If I do this:

driver.execute_script("prompt('Enter smth','smth')") 

I get a good hint, but I cannot use this value. Is there a way to show the user an input field and use the value entered there?

EDIT: This is my scenario:

 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 comes out 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 doing wrong?

EDIT: I tried to do as prestomanifesto suggested, here is the conclusion:

 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 
+6
source share
6 answers

You are using javascript tooltip correctly. But the value of the invitation field must be assigned to a global variable, and then you can use this variable later. something like that:

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

and then get the value from the same global variable.

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

you will probably need to return the result.

Hope this helps.

+2
source

Tkinter is a GUI-based library that you can use to get input from the user at runtime. This will make the program wait until the user enters information. For a pre-designed dialog box, you can click on this link . Although it’s too late, but maybe it will help someone else.

+1
source

Why not return the value directly?

 if __name__ == "__main__": driver = Firefox() a = driver.execute_script("return prompt('Enter smth','smth')") print "got back %s" % a 

Works for me in C #. Admittedly, this is a slightly older version of Selenium, but I would not expect the execute_script function to change much.

0
source

You can use the proposed technique here.

Main idea:

  • Issue Selenium commands to the point where you want to capture user input.
  • Get user input in console window using raw_input()
  • Continue running Selenium commands.

In Python, for example:

 #Navigate to the site driver.Navigate().GoToUrl("http://www.google.com/") #Find the search box on the page queryBox = self.driver.FindElement(By.Name("q")) #Wait for user text input in the console window text = raw_input("Enter something") #Send the retrieved input to the search box queryBox.SendKeys(text) #Submit the form queryBox.Submit() 
0
source

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 
0
source

If you guys use selenium 2.28 like me, this will do the trick, like @ Baz1nga say

 //Open the prompt inbox and setup global variable to contain the result WebDriver driver = new FirefoxDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.promptResponse = prompt(\"Please enter captcha\");"); //Handle javascript prompt box and get value. Alert alert = driver.switchTo().alert(); try { Thread.sleep(6000); } catch (Exception e) { System.out.println("Cannot sleep because of headache"); } alert.accept(); String ret = (String) js.executeScript("return window.promptResponse;"); 
-1
source

Source: https://habr.com/ru/post/901761/


All Articles