2

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?

ddavison
  • 28,221
  • 15
  • 85
  • 110
Buras
  • 3,069
  • 28
  • 79
  • 126

2 Answers2

3

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.*");
ddavison
  • 28,221
  • 15
  • 85
  • 110
  • If you don't absolutely a pattern, you could simply do `if (driver.getCurrentUrl().contains("Servlet")) { ... }`. – LaurentG Sep 27 '13 at 18:22
  • of course not but i highly recommend using regex. Gives you a lot of flexibility – ddavison Sep 27 '13 at 18:23
  • This function is pseudo-ey, and you'll need to port it to fit your needs. this will not work if you copypasta it, but it's pretty close – ddavison Sep 27 '13 at 18:24
  • Thank you for reply. However, it appears that method `getDriver()` doesn not exist. Do I have to import some jars? – Buras Sep 27 '13 at 18:58
  • take a closer look at the comments above - this was taken from my *own* impelementation. you'll have to port it to what you need. `getDriver()` obviously equates to `driver` – ddavison Sep 27 '13 at 19:14
0
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

Rahul Rana
  • 41
  • 6