driver.switchTo().window() using window title?

5,347 views
Skip to first unread message

confusa

unread,
Jun 10, 2011, 12:56:49 PM6/10/11
to webdriver
I know you can use this method with the window handle, but that isn't
overly useful for me. Is it possible to use this method to select a
window by it's HTML title? Another method maybe?

QA Automation at nixsolutions.com

unread,
Jun 10, 2011, 1:39:43 PM6/10/11
to webd...@googlegroups.com
    public bool SwtichToWindowUseingTitle(IWebDriver driver, string strTitle)
        {
            var currentWindow = driver.GetWindowHandle();
            var availableWindows = driver.GetWindowHandles();
            if (availableWindows.Count != 0)
            {
                foreach (string windowId in availableWindows)
                {
                    if (driver.SwitchTo().Window(windowId).Title == strTitle)
                        return true;
                }
                {
                    driver.SwitchTo().Window(currentWindow);
                    return false;
                }
            }
            else
                return false;
        }

QA Automation at nixsolutions.com

unread,
Jun 10, 2011, 1:13:38 PM6/10/11
to webdriver
Hi,

in order to switch between browser's windows it would be better to
create your own method which will be switches to necessary window
automatically.
Something like that:
Please let me know if you have any additional questions.

__________________________
Best regards,

Dmitry

nixsolutions

QA Automation at nixsolutions.com

unread,
Jun 10, 2011, 1:25:54 PM6/10/11
to webdriver
Hope it helps.

On Jun 10, 7:56 pm, confusa <materiaconf...@gmail.com> wrote:
Message has been deleted

confusa

unread,
Jun 10, 2011, 3:56:46 PM6/10/11
to webdriver
Works, thanks! Slight Java tweak:

protected boolean switchToWindowUsingTitle(WebDriver driver, String
title) {
String currentWindow = driver.getWindowHandle();
Set<String> availableWindows = driver.getWindowHandles();
if (!availableWindows.isEmpty()) {
for (String windowId : availableWindows) {
if (driver.switchTo().window(windowId).getTitle().equals(title)) {
return true;
} else {
driver.switchTo().window(currentWindow);
}
}
}
return false;
}



On Jun 10, 10:39 am, "QA Automation at nixsolutions.com"
Reply all
Reply to author
Forward
0 new messages