【问题标题】:After opening a new tab in Selenium WebDriver, how to make it the visible/active tab in my browser?在 Selenium WebDriver 中打开一个新选项卡后,如何使其成为浏览器中的可见/活动选项卡?
【发布时间】:2014-11-28 10:55:39
【问题描述】:

我正在使用以下 WebDriver Java 代码打开一个新选项卡(当然可能有更好的方法来做到这一点):

        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");

而且它工作得很好,我所做的任何进一步的操作都会应用于那个新标签,但它在我的屏幕上不可见,我仍然可以看到打开的第一个标签。有什么方法可以更改在我的窗口中可见的选项卡吗?

谢谢

【问题讨论】:

    标签: java selenium selenium-webdriver tabs webdriver


    【解决方案1】:

    要切换标签,请使用

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

    【讨论】:

    • 我尝试了上述方法,不幸的是我仍然可以看到我原来的标签。
    • 是的,因为这只是将您切换到一个新标签。您需要删除以前的标签还是什么?
    • 我希望我切换到的选项卡成为我可以在窗口中实际看到的选项卡。
    • 好的,只需使用以下代码替换上面的代码:newTab.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); 它将关闭当前打开的选项卡,我认为这是原始选项卡,然后单击并打开新选项卡。
    • @Subh F4 将自行关闭标签
    【解决方案2】:

    其实,在标签之间切换是没问题的。但是在它们之间切换控制是不可能的,因为即使您打开一个新标签,它仍然在主窗口内,除非您关闭原始标签,否则控制不会切换到新标签。

    那么,你可以做的是在新窗口中打开链接,并获取新打开的窗口的窗口句柄。然后,您可以在两个窗口之间切换(以及切换手柄)并执行必要的操作。

    以下是使用“google.com”网站的示例。请执行代码并检查流程。你会明白我上面的意思:

    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
    
        }
    }
    

    【讨论】:

    • @Ash:如果你不明白上面代码中发生了什么,请告诉我。
    • 差不多一年后 - 切换控制两个当前活动的选项卡是这样完成的:driver.SwitchTo().DefaultContent()。另外,我通过发送在选项卡之间切换:Ctrl +(选项卡编号)。这样,您可以循环播放或直接转到选项卡。
    • 太巧妙了!非常感谢@BClaydon。你让我很开心.. :)
    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 2018-08-29
    相关资源
    最近更新 更多