0

The code that worked with Selenium 2 stopped working with Selenium 3.

This code does not work any more neither in Chrome nor in Firefox.

driver.get("http://the-internet.herokuapp.com/windows");
String firstWindow = driver.getWindowHandle();
driver.findElement(By.linkText("Click Here")).click();
driver.switchTo().window(firstWindow);

I found that for Firefox there is a workaround:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);

However, nothing works for Chrome.
Please help

Vladimir
  • 630
  • 3
  • 12
  • 26
  • Possible duplicate of [switch tabs using Selenium WebDriver with Java](http://stackoverflow.com/questions/12729265/switch-tabs-using-selenium-webdriver-with-java) – Jainish Kapadia Feb 24 '17 at 07:36
  • Can you provide an exception? What exactly "does not work anymore"? – Robert G Feb 24 '17 at 08:08
  • To jainish. If you look at the link you've provided, it used the code that does not work with Selenium 3 as I pointed out. – Vladimir Feb 24 '17 at 08:09
  • To Robert G. There is no exception. You just stay in the second tab and do not switch to the first tab in Chrome. – Vladimir Feb 24 '17 at 08:10

1 Answers1

1

I think you are indeed suffering from a new bug (or maybe intended behaviour?).

After checking out your code and testing it, I noticed that even though a new tab gets opened and focused in Chrome the driver does not automatically switch to that tab.

You have to add .switchTo().window(secondWindow) so your driver switches to the new (and active) tab.

So .switchTo() doesn't focus the tab, which the driver is using and thus it looks like you just stay in the second tab, even though the driver itself switched back to the first window.

Here is some code that might help you understand the issue:

    driver.get("http://the-internet.herokuapp.com/windows");
    String firstWindow = driver.getWindowHandle();
    System.out.print("First windowhandle: " + firstWindow + "\n");

    System.out.print("Clicking on 'Click Here' \n");
    driver.findElement(By.linkText("Click Here")).click();

    Set<String> stringSet = driver.getWindowHandles();
    System.out.print("All windowhandles: " + stringSet + "\n");
    List<String> handles = new ArrayList<>(stringSet);

    System.out.print("Switching to new window \n");
    driver.switchTo().window(handles.get(1));
    //wait for Title to change to second tab title
    new WebDriverWait(driver, 10).until(ExpectedConditions.titleIs("New Window"));

    System.out.print("Second (and current) windowhandle: " + driver.getWindowHandle() + "\n");

    System.out.print("Switching back to first Window. \n");
    driver.switchTo().window(firstWindow);

    //wait for Title to change back to first title
    new WebDriverWait(driver, 10).until(ExpectedConditions.titleIs("The Internet"));

    System.out.print("Last used windowhandle: " + driver.getWindowHandle() + "\n");

If you run this code you will notice two things:
a) the driver will switch to the correct tab (see the WindowHandle output)
b) the currently used tab the driver uses will not be properly focused in Chrome

This does seem to be a bug.

Robert G
  • 928
  • 7
  • 9
  • Thank you, Robert for response. I did the same investigation as in your code snippet before posting my question. This is a bug! Who will fix it? :-) Selenium 3 is really buggy. For example, Firefox driver did not implement the Actions class. – Vladimir Feb 24 '17 at 19:09