0

I have opened the link now i am clicking on login link on that page, By clicking on login button its opening in a new tab automatically. I have to fill the form in that tab. For that i tried following code:

Actions act = new Actions(Initialsetupfirefox.driver);
new Actions(Initialsetupfirefox.driver) .keyDown(Keys.CONTROL).click(Initialsetupfirefox.driver.findElement(By.xpath("//a[contains(text(),'LOGIN')]")))
         .keyUp(Keys.CONTROL)
         .perform();
        Thread.sleep(3000);

new Actions(Initialsetupfirefox.driver).sendKeys(Initialsetupfirefox.driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(Initialsetupfirefox.driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();

By this i am able to switch the focus but not able to do anything in this tab. Please suggest

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
liki
  • 33
  • 3
  • 13
  • You need to switch that newly opened tab before interacting to the element, follow [this link for switching window](http://stackoverflow.com/questions/9588827/how-to-switch-to-the-new-browser-window-which-opens-after-click-on-the-button) – Saurabh Gaur Sep 12 '16 at 11:48
  • but i am doing switching tabs – liki Sep 14 '16 at 11:41

3 Answers3

4

you can do like this:

    Set<String> handles = driver.getWindowHandles();
    String currentHandle = driver.getWindowHandle();
    for (String handle : handles) {

     if (!handle .equals(currentHandle))
     {
         driver.switchTo().window(handle);
     }
   }

   //fill your form in the other tab
   ......

   //go back to first tab if you want
    driver.switchTo().window(currentHandle);
Hezi Israeli
  • 389
  • 1
  • 2
  • 14
1

This it the code when using selenium with Node.js and Typescript

 const mainHandle = await this.driver.getWindowHandle();
 // load new tab here .. 
 const allHandles = await this.driver.getAllWindowHandles();
 for (const handle of allHandles) {
   if (handle !== mainHandle) {
     await this.driver.switchTo().window(handle);
   }
 }
Seagull
  • 1,063
  • 1
  • 11
  • 18
0

Get the browser-tabs and do as you like:

List<String> browserTabs = Lists.newArrayList(driver.getWindowHandles());

driver.switchTo().window(browserTabs.get(1)); // Switch to second tab
// Do something on second tab here...

driver.switchTo().window(browserTabs.get(0)); // Return to first tab
// Do something on first tab here...