Reputation: 3099
I have three windows open as a result of Selenium manipulations. I need to switch to the the window who's URL contains a substring "Servlet"
driver.switchTo().window("*Servlet*");
How can I code it correctly?
Upvotes: 2
Views: 4509
Reputation: 41
protected void switchTabsUsingPartOfUrl(String platform) {
String currentHandle = null;
try {
final Set<String> handles = driver.getWindowHandles();
if (handles.size() > 1) {
currentHandle = driver.getWindowHandle();
}
if (currentHandle != null) {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
break;
}
}
} else {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform)) {
break;
}
}
}
} catch (Exception e) {
System.out.println("Switching tabs failed");
}
}
Just call this method and pass a substring of url of the tab you want to switch to
Upvotes: 0
Reputation: 29042
I've done a similar thing recently.. here is an excerpt. (i ported it from doing title, to Url.)
driver.switchTo().window("*Servlet*");
Unfortunately, it's not that easy.. we can't specify wildcards there.. so let's make some functions for it -
/**
* Waits for a window with a desired url to come up.<br>
* If it does not come up in 5 seconds, it will fail.
* <br>
* <br>
* This method will set the current active window to the window requested. If you need to switch back to the previous window, use {@link #switchToWindow(String)}
* @param regex The title of the window. Regex enabled.
* @return
*/
public void waitForWindow(String regex) {
Set<String> windows = getDriver().getWindowHandles();
for (String window : windows) {
try {
driver.switchTo().window(window);
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find())
return switchToWindow(regex);
} catch(NoSuchWindowException e) {
if (attempts <= 5) {
attempts++;
Thread.sleep(1);
return waitForWindow(regex);
} else {
fail("Window with url: " + regex + " did not appear after 5 tries. Exiting.");
// found
}
}
}
// when we reach this point, that means no window exists with that title..
if (attempts == 5) {
fail("Window with title: " + regex + " did not appear after 5 tries. Exiting.");
return (T) this;
} else {
System.out.println("#waitForWindow() : Window doesn't exist yet. [" + regex + "] Trying again. " + attempts + "/5");
attempts++;
return waitForWindow(regex);
}
}
Switch to window would be,
/**
* Switches the current window based on title.
* @param regex A regular expression window title.
* @return
*/
public void switchToWindow(String regex) {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
getDriver().switchTo().window(window);
System.out.println("#switchToWindow() : url=" + driver.getCurrentUrl());
p = Pattern.compile(regex);
m = p.matcher(driver.getCurrentUrl());
if (m.find())
// found
}
fail("Couldn't switch to window with urlregex: " + regex + "!");
}
In your example, it'd be,
waitForWindow(".*Servlet.*");
switchToWindow(".*Servlet.*");
Upvotes: 3