Search code examples
javaselenium-webdriverwindow-handles

Windows handling in Selenium webdriver using java


I have a scenario as below

1. Login to the application
2. click on a button (say Buy)
3. This will take me to a new window opened with a new URL automatically
4. Perform actions in the new window
5. Quit

Kindly please provide the exact code to work on this. I tried with the available code that exists in the website which didnt work for me


Solution

  • You can try on following pattern:-

       Webdriver driver = new ChromeDriver();
       driver.get("URL of application");
       driver.findElement(By.id("username").sendKeys("user1");
       driver.findElement(By.id("password").sendKeys("pass1");
       driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
       driver.findElement(By.xPath("xpath of button").click();
    

    //now you can switch to pop-up and accordingly accept or dismiss it

       driver.switchTo().alert().accept();
    
       driver.quit();
    

    In case you provide the SO community the URL of application then only complete code can be provided.