3

Is there any way to get the text from an alert in JavaScript?

Below I have attached the screenshot of an alert.

Actually I am trying to automate a website using selenium and java and selenium isn't able to handle such alert.

I wonder whether the JavaScript has solution to such alerts.

I am not aware of JavaScript syntax and code styling so posting only a question. Please excuse me for this.

driver.switchTo.alert().getText() 

only works for if it is a JS alert,if it is browser alert then it doesn't work

ALERT EXAMPLE:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Good Vibes
  • 163
  • 2
  • 12
  • 1
    Possible duplicate of [How to check if an alert exists using WebDriver?](https://stackoverflow.com/questions/11467471/how-to-check-if-an-alert-exists-using-webdriver) – Evan Knowles Mar 20 '19 at 07:18
  • @Evan..driver.switchTo.alert().getText() only works for if it is a JS alert,if it is browser alert then it doesn't work – Good Vibes Mar 20 '19 at 11:58
  • linking questions: https://stackoverflow.com/questions/55253818/how-to-get-text-from-leave-site-alert – jithinkmatthew Mar 20 '19 at 12:47

2 Answers2

1

You should try this, with Selenium:

Alert alert = driver.switchTo().alert();
alert.getText();
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
0

The only way I know to do this within javascript is using the confirm popup. It has generic 'OK' and 'Cancel' buttons which I don't believe can be edited and is formatted according to the users web browser. To use the confirm method you write:

if (confirm("Do you really want to leave here?"){
   //do whatever
}
Mick
  • 79
  • 1
  • 6