-1

I'm new to selenium testing. so bear with me. I have a web page where when i click a button, a new browser window is opened and I have to fill some stuff there. I was able to to the first part (selecting the button and opening the new browser window).

driver.findElement(By.xpath("//xpath/to/button/id")).click();

but the problem occurs when I try to fill the stuff in that newly popped up browser. I'm using the same driver (may be this could be the problem). when I print the current url path driver.getCurrentUrl(); it shows the previous url (not the redirected one). How to set the newly opened browser to the driver

Can someone help me with this (any tutorial would be nice). Thanks in advance

Chamila Adhikarinayake
  • 3,588
  • 5
  • 25
  • 32

2 Answers2

1

Try this:

String baseWindowHdl = driver.getWindowHandle();
driver.findElement(By.xpath("//xpath/to/button/id")).click();
//Go to New Window
for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }
enter your code here for new window

// Close pop-up
    driver.close();

    // Switch back to base window
    driver.switchTo().window(baseWindowHdl);
qaRookie
  • 105
  • 1
  • 2
  • 10
0

You need to make sure that you handle your own windows with Selenium. By Default, if you have these statements:

driver.open("some_url");
driver.getCurrentUrl(); // returns "some_url"
driver.open("some_other_url");
driver.getCurrentUrl(); // returns "some_other_url" 

From that, you can gather that when you call open() from Selenium, it opens in the same window.

What you are doing, is trying to operate with another window. There are several answers out there and other resources you can use to find out how to do exactly what you want to do.

Community
  • 1
  • 1
ddavison
  • 28,221
  • 15
  • 85
  • 110