0

We are using Selenium to open certain webpages, but when opening the webpage, there is a popup that comes up and we are unable to handle the popup using selenium code. Below is the sample code for the same. I'm trying to use the alert method, but the system is unable to detect the alert

System.setProperty("webdriver.chrome.driver","D:\\Test\\drivers\\chromedriver.exe");
        WebDriver driver =  new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://the-internet.herokuapp.com/basic_auth");
        Alert alert=driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.dismiss();

I put a debug point on the above code. and the code gets stuck at the line no.4 driver.get(). Unless some action is done on the loaded page like manually clicking on the cancel button, the control does not move to the next line.

Help required on

  • Using selenium, how do I handle the popup when loading the webpage? I'm unable to detect the Popup elements via selenium
  • I guess this is a javascript popup that is coming up on page load. How do I handle javascript popup on page load via selenium?

Any help on this is much appreciated. Thanks in Advance.

BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
vr3w3c9
  • 1,118
  • 8
  • 32
  • 57
  • Have you tried waiting like 200 ms after page load – DownloadPizza Jul 18 '19 at 09:08
  • When you say it gets stuck on line 4, do you mean thats where the test fails? – Stephen K Jul 18 '19 at 09:15
  • Yes. Actually, you can try accessing this url "https://the-internet.herokuapp.com/basic_auth" via chrome browser so you will be able to get the problem that Im facing. When you try to access the url, a pop up comes up. Im not sure how to handle it via selenium. Because I could not detect the popup element via selenium – vr3w3c9 Jul 18 '19 at 09:20
  • oh then you don't need to dismiss it, you can just navigate to the url with the credentials in the url. see answer – Stephen K Jul 18 '19 at 09:43

1 Answers1

1

If you need to authenticate with basic auth before being able to view the page, you can navigate to the url with the credentials in the url so:

https://admin:admin@the-internet.herokuapp.com/basic_auth

syntax:

<protocol>://<username>:<password>@<url>

Just tried it with that site you're testing with and was able to hit the page after authentication fine

Stephen K
  • 85
  • 7
  • We have tried this option as well. But this does not work for the urls that we are trying to access.The sample link I specified early works but not the intranet sites that we are trying to access. – vr3w3c9 Jul 18 '19 at 09:59
  • I've literally just tried it myself and saw the expected page. using code `driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth")` – Stephen K Jul 18 '19 at 10:03
  • As specified above, the sample url that I specified works but not the other pages for which we are trying to setup. We would like to know how to handle the popups via selenium on page load. – vr3w3c9 Jul 18 '19 at 10:03
  • Right okay sorry, apologies, glazed over first comment – Stephen K Jul 18 '19 at 10:08