0

I need to get the default value from prompt via selenium. If this is my prompt :

prompt("Please enter your name:", "Harry Potter");

I want to get "Harry Potter" to my String

I tried use this :

alert.Text;

But i get the first text "Please enter your name:"

The prompt :

var person = prompt("Please enter your name:", "Harry Potter");

my code :

String name = driver.switchTo().alert().getText();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
daniklo
  • 31
  • 1
  • 5

1 Answers1

0

Here is the solution.

 // switch to alert
 Alert alert = driver.switchTo().alert();
 // accept the prompt without changing anything
 alert.accept();
 // now get the default value
 JavascriptExecutor js = (JavascriptExecutor) driver;
 String defaultVal = (String) js.executeScript("return person");
 //print the default value from prompt
 System.out.println(defaultVal);
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks a lot but it does not work i get error message "javascript error: person is not defined" – daniklo Jun 06 '19 at 06:43
  • `var person = prompt("Please enter your name:", "Harry Potter");` is this the line that you see in the prompt line? I mean does the prompt return the value into person? – supputuri Jun 06 '19 at 12:12