power_settings_new

change focus to a new popup tab in Selenium

by
asked
0 votes
2.1k views

I have a link on a page (say linkA) that opens a new page in a new tab. The new tab is displayed when linkA is clicked. I then want to interact with the new page.

Here is my selenium script:

  • click linkA
  • pause 5000
  • selectWindow Title
  • click linkB (note: linkB is on the new page)

Selenium cannot identify the new tab. It reports:

[warn] Link has target '_blank', which is not supported in Selenium! Randomizing target to be: selenium_blank24003

Is there any way to tell Selenium to interact with the displayed tab?

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

answered by (327 points)
selected by
0 votes
 
Best answer

you will have to use driver switch to.

You can switch between windows as below:

//Store the current window handle
String winHandleBefore = driver.getWindowHandle();

//Perform the click operation that opens new window

//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

// Perform the actions on new window

//Close the new window, if that window no more required
driver.close();

//Switch back to original browser (first window)

driver.switchTo().window(winHandleBefore);

//continue with original browser (first window)
add
...