Waiting for a new tab/window to open

6,090 views
Skip to first unread message

gillyb

unread,
Nov 23, 2011, 9:07:53 AM11/23/11
to Selenium Users
I created a test that clicks on a link that opens a new tab. I want to
verify that the new tab is on the right url, so i switch to the new
WebHandle like this :

var newWindow = WebDriver.WindowHandles[1];
WebDriver.SwitchTo().Window(newWindow);

The problem is that i get an error like this :
System.InvalidOperationException: respond.session.getWindow() is null
at
OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response
errorResponse)

When i run this test on my computer it works, but on the build server
at my job it doesn't, and the error is thrown.
This leads me to believe that the error is because the build server is
slower, and therefore the WindowHandle isn't created when i try to
switch to it.

I added Thread.Sleep(), and it fixed the problem, but I was wondering
if there was a better way to fix this problem ??

Mark Collin

unread,
Nov 23, 2011, 9:23:47 AM11/23/11
to seleniu...@googlegroups.com
Try this:

public void waitForNumberOfWindowsToEqual(final int numberOfWindows) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.getWindowHandles().size() == numberOfWindows);
}
});
}

Set the number of windows you are expecting using the above function and
then use the line of code you have shown below.

--
You received this message because you are subscribed to the Google Groups
"Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to
selenium-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

gillyb

unread,
Nov 23, 2011, 9:39:58 AM11/23/11
to Selenium Users
I probably forgot to state that im working in C#.
Do you know how i can write that in C# ?

Thanks!

> For more options, visit this group athttp://groups.google.com/group/selenium-users?hl=en.


>
> --
> This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
>

> If you have received this email in error please notify postmas...@ardescosolutions.com

Moises Siles

unread,
Nov 23, 2011, 9:49:50 AM11/23/11
to seleniu...@googlegroups.com
pretty much it should be the same, just change the line

wait.Until(driver => webDriver.WindowHandles.Count() == numberOfWindows);

famzy

unread,
Feb 13, 2020, 10:50:11 PM2/13/20
to Selenium Users
Thank you Mark. This solution works perfectly in 2020.


For more options, visit this group at
http://groups.google.com/group/selenium-users?hl=en.

Reply all
Reply to author
Forward
0 new messages