After opening a new tab in Selenium WebDriver, how

2020-05-09 11:44发布

问题:

I am using the following WebDriver Java code to open a new tab (granted there are possibly better ways to do this) :

        WebElement link = driver.findElement(By.id("home_button")); 
        Actions newTab = new Actions(driver); 
        newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
        ArrayList<String> openTabs = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(openTabs.get(1));
        driver.navigate().to("http://google.com");

And it works perfectly well, and any further actions I do are applied to that new tab, but it is not visible on my screen, I can still see the first tab that was opened. Is there any way to change which tab is visible in my window?

Thanks

回答1:

For switching tabs, use

newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();


回答2:

Actually, switching between tabs is fine. But switching control in-between them, is not possible because even if you open a new tab it is still inside the main window, and unless you close the original tab, the control won't shift to a new one.

So, what you can do is open the link in a new window, and get the window handle of the newly opened window. Then, you can switch between the two windows (alongwith switching handles) and perform the necessary action(s).

Below is an example using "google.com" site. Please execute the code and check the flow. You will understand what I meant above:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Testing_window_switching{

    public static void main(String args[]) throws InterruptedException{

        WebDriver driver = new FirefoxDriver(); //Opening firefox instance

        driver.manage().window().maximize(); //maximizing window
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds


        driver.get("http://google.co.in/");

        driver.findElement(By.id("gbqfq")).sendKeys("stackoverflow");

        driver.findElement(By.xpath("//ul[@role='listbox']//div[.='stack overflow']")).click();

        String parentWindow = driver.getWindowHandle();//Getting parent window handle
        System.out.println("Parent Handle is: "+parentWindow);


        Actions newTab= new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//div[@class='srg']//a[.='Stack overflow - Wikipedia, the free encyclopedia']"));
        //Opening the link having text 'Stack overflow - Wikipedia, the free encyclopedia']' in new window
        newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

        String childWindow = null;
        for(String childs: driver.getWindowHandles()){
            if(!childs.equals(parentWindow)){
                childWindow = childs;
                break;
            }
        }

        System.out.println("Child handle is: "+ childWindow);

        driver.switchTo().window(childWindow); //To switch handle to child window   

        driver.findElement(By.xpath("//a[.='Stack overflow (disambiguation)']")).click();
        driver.navigate().back();

        newTab.sendKeys(Keys.chord(Keys.ALT,Keys.TAB)).perform(); //To switch to main window
        driver.switchTo().window(parentWindow); //To switch current handle to parent
        System.out.println("Switched to parent window");

        driver.findElement(By.xpath("//div[@class='srg']//a[.='Blog – Stack Exchange']")).click();// Click on the link 'Blog – Stack Exchange' in parent window

        newTab.sendKeys(Keys.chord(Keys.ALT,Keys.TAB)).perform(); //To switch to child window.  
        driver.switchTo().window(childWindow); //To switch current handle to child
        System.out.println("Switched to child window");

        driver.findElement(By.xpath("//li[@id='n-mainpage-description']/a")).click();//Clicking on the link 'Main Page' in the Child window

        Thread.sleep(10000);//Sleep time of 10 seconds, not necessary but just to give some time before quitting

        driver.quit(); //Closing all instances of browser

    }
}