11

I have clicked an image in a slide show and it generates a new window. Once the window opens I need to bring focus on new window and do some stuff. I have attached my code below, but it is not successful.

@Test
public void testCreateAccount() throws Exception{

//OPEN manorama online  url
driver.navigate().to("http://www.manoramaonline.com/cgi-bin/MMOnline.dll/portal/ep/home.do?tabId=0&BV_ID=@@@");

// to click a link inside a slide show

while (!driver
        .findElement(
                By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
        .isDisplayed()) {
}
driver.findElement(
        By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
        .click();



String newwindowTitle = driver.getTitle();

switchWindow(newwindowTitle);

}

protected static boolean switchWindow(String title) throws IOException {

    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;   
    }
  • You're making an assumption that titles of pages are different/unique. Is it true for your app? – dzieciou Oct 20 '12 at 1:51
11

A possibly much simpler solution would be this:

driver.SwitchTo().Window(driver.WindowHandles.Last());

.Last() will just switch to the most recently opened window, which should be the one that just opened when you clicked the link.

3

I think that you're probably over complicating the problem in your switchWindow method.

If I understand you correctly, all that you want to do is iterate through the open windows, compare the title of each window, and if it's the right one, return true.

C# example

protected static Boolean SwitchWindow(string title)
            {
                var currentWindow = driver.CurrentWindowHandle;
                var availableWindows = new List<string>(driver.WindowHandles);

                foreach (string w in availableWindows)
                {
                    if (w != currentWindow)
                    {
                        driver.SwitchTo().Window(w);
                        if (driver.Title == title)
                            return true;
                        else
                        {
                            driver.SwitchTo().Window(currentWindow);
                        }

                    }
                }
                return false;
            }
1

When you only have a URL, this may be a helpful solution:

                   public void SwitchTo(string prefix)                 
                   {                        
                    var handlers = driver.WindowHandles;

                        foreach (var handler in handlers)
                        {
                            driver.SwitchTo().Window(handler);
                            if (driver.Url.Contains(prefix)) return;                                     
                        }
                   }
0

For some browsers it happens that once mouse hover action is performed, but the menu list disappear quickly before Selenium identify the next sub menu item. In that case it is better to use perform() action on the main menu to hold the menu list till the time Selenium identify the sub menu item and click on it.

Here

WebElement xWL = driver.findElement(By.xpath("x path text"));

Actions xAct = new Actions(driver);

Instead of this:

xAct.moveToElement(xWL).build().perform();

below code will resolve the "element not visible" issue

xAct.moveToElement(xWL);
xAct.click();
xAct.perform();
0

Using Ruby and Watir-webdriver you just have to use this code

browser.windows.last.use

this will set focus to newly or last opened tab or window,

and after completion of use of this new tab/window just use this to close that tab/window

browser.windows.last.close

Or if you want switch back to previous tab/window, you will be able to find the window by :title, :url, or :index.

browser.window(:index => 1).use
browser.window(:url => /closeable\.html/).use
browser.window(:title => "closeable window").use

Not the answer you're looking for? Browse other questions tagged or ask your own question.