-2

I want to handle this exception in JAVA & SELENIUM

In my code i did this:

        Alert alert = driver.switchTo().alert(); alert.accept(); 
        driver.findElement(By.xpath("//*[@id=\"btnGoConfirm\"]")).click();

The problem is that sometimes the popup does not appear and the exception in thrown because it cannot click on anything at all, do you get me?

How should i handle this particular situation?

JustToKnow
  • 785
  • 6
  • 23
  • Maybe you can add those 2 lines into a try/catch block. In the try block add those 2 lines, in the except block do nothing. If alert appears it will click the button, if not your script will continue. – Jaky Ruby Nov 10 '22 at 18:10
  • Hey @JakyRuby Jaky, thanks for commenting!. how would you do it in this case? I'm kinda confused – JustToKnow Nov 10 '22 at 18:12
  • Maybe the answer could be a solution – Jaky Ruby Nov 10 '22 at 18:13

1 Answers1

1

Something like:

try {
    Alert alert = driver.switchTo().alert(); alert.accept(); 
    driver.findElement(By.xpath("//*[@id=\"btnGoConfirm\"]")).click();
}
catch(Exception e) {
    System.out.println("I got an exception, but it is only because alert did not appear, so I continue with my script")
}
Jaky Ruby
  • 1,409
  • 1
  • 7
  • 12
  • I'm gonna try it, Jaky. I don't want the problem to stop running if the pop up does not appear – JustToKnow Nov 10 '22 at 18:16
  • Jaky, could you please take a look at this?: https://stackoverflow.com/questions/74392304/not-able-to-pass-the-result-from-a-sql-query-to-a-method-in-java – JustToKnow Nov 10 '22 at 18:18
  • 1
    Actually I am not a Java developer, I just though that maybe this could help you with your script, but I can not help you a lot with Java...with selenium maybe a little bit more. – Jaky Ruby Nov 10 '22 at 18:20
  • Alright, pal :) – JustToKnow Nov 10 '22 at 18:20