1

Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.

I saw that I should use this:

driver.switchTo().window(windowName);

but what is windowName?

budi
  • 6,351
  • 10
  • 55
  • 80
Light_User
  • 83
  • 2
  • 11

1 Answers1

0

You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.

Here is a sample working code in java:

    String parentHandle = driver.getWindowHandle(); // get the current window handle
    System.out.println(parentHandle);               //Prints the parent window handle 
    String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
    anchor.click();                                 //Clicking on this window
    for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
        System.out.println(winHandle);
        driver.switchTo().window(winHandle);        // switch focus of WebDriver to the next found window handle (that's your newly opened window)              
    }
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
    driver.close();                                 // close newly opened window when done with it
    driver.switchTo().window(parentHandle);         // switch back to the original window

Hope this helps!

Fahim Hossain
  • 1,671
  • 13
  • 16
  • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it – Light_User Aug 27 '14 at 15:07
  • Selenium holds the name of the windows in some strings called *handles*. In C# we don't use getters and setters, but Properties: `driver.WindowHandles` is the way to get all the handles, just go for the `.Last()` and you should get what you want –  Aug 27 '14 at 15:10
  • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last()); – Fahim Hossain Aug 27 '14 at 15:29
  • this is how my code looks like ( open new tab + switch ) `IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());` – Light_User Aug 27 '14 at 15:34
  • Buy the way , I didn't get the No Window found , but there was no result either – Light_User Aug 27 '14 at 15:39
  • Assuming 1. you are opening the new window first before you are using driver.SwitchTo().Window(driver.WindowHandles.Last()) 2. Then you are trying to do anything on this new window then it should work. Then it should work. Lemme know – Fahim Hossain Aug 27 '14 at 15:39
  • well yes my cone first sends CTRL+T ( to open new tab ) then I want to navigate – Light_User Aug 27 '14 at 15:49