无法使用java中的webdriver切换到chrome中的“当前活动选项卡”

时间:2016-03-11 17:40:01

标签: java selenium selenium-webdriver tabs selenium-chromedriver

我无法使用WebDriver切换到Chrome中的“当前有效标签”。我试过以下几点:
我们可以切换到标签,但我们不知道哪一个是当前有效标签

  1. 我已经尝试了driver.switchTo().activeElement()但是没有任何帮助
  2. 我尝试了driver.switchTo().Window(""),但无效
  3. 尝试了以下代码段。
  4. Set<String> allWindowHandles = driver.getWindowHandles();  
      for (String windowHandle : allWindowHandles) {
       if (!windowHandle.equals(<cannot get the active window handle>)) {
          driver.switchTo().window(windowHandle);
       }
    }
    

    但是,获取当前活动标签句柄是个问题。

1 个答案:

答案 0 :(得分:0)

<强>更新 -

以下是使用多个窗口/标签(即两个或两个以上的窗口/标签)的工作代码(C#,使用Chrome测试)。 使用此方法,您可以从任何选项卡切换到任何其他选项卡。 (代码未优化,您可以进一步优化。)

        [Test]
    public void MultipleWindows()
    {
        IWebDriver driver = new ChromeDriver(@"G:\Selenium\Tools\Selenium\");
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));

        List<string> allWindowHandles = new List<string>();
        List<string> allCapturedWindowHandles = new List<string>();
        string currentWidowHandle = null;

        //Navigate to URL
        driver.Navigate().GoToUrl(@"http://www.w3schools.com/js/");
        driver.Manage().Window.Maximize();

        //Get First Window Handle
        currentWidowHandle = driver.CurrentWindowHandle;
        allCapturedWindowHandles.Add(currentWidowHandle);

        Thread.Sleep(2000); //Static wait is not recommended
        Console.WriteLine("\nFirst Tab Title: " + driver.Title);
        Console.WriteLine("First Tab URL: " + driver.Url);

        //Code to open new tab
        //Click on the link to open new tab
        IWebElement TryItYourselfLinkJS = driver.FindElement(By.CssSelector("a[href='tryit.asp?filename=tryjs_myfirst']"));
        TryItYourselfLinkJS.Click();

        Thread.Sleep(5000); //Static wait is not recommended

        //Store all window handles in a list
        allWindowHandles = driver.WindowHandles.ToList();

        //Get new window handle, the one which is not equals to first tab.
        for (int i = 0; i < allWindowHandles.Count; i++)
        {
            int k = 0;

            for (int j = 0; j < allCapturedWindowHandles.Count; j++)
            {
                if (allWindowHandles[i] == allCapturedWindowHandles[j])
                {
                    k++;
                }
            }

            if (k == 0)
            {
                allCapturedWindowHandles.Add(allWindowHandles[i]);
            }

        }

        //Switch to new window handle i.e. second tab
        driver.SwitchTo().Window(allCapturedWindowHandles[1]);

        Thread.Sleep(6000); //Static Wait is not recommended
        Console.WriteLine("\nSwitched to Second Tab. \nSecond Tab Title: " + driver.Title);
        Console.WriteLine("Second Tab URL: " + driver.Url);

        //Swich back to first window
        driver.SwitchTo().Window(allCapturedWindowHandles[0]);

        Thread.Sleep(5000); //Static wait is not recommended
        Console.WriteLine("\nSwitched to First Tab. \nFirst Tab Title: " + driver.Title);
        Console.WriteLine("First Tab URL: " + driver.Url);

        //Code to open third tab. 
        IWebElement topNavigationCSS = driver.FindElement(By.CssSelector("div#topnav>div>div>a:nth-of-type(4)"));
        topNavigationCSS.Click();

        //Click the link to open third tab
        IWebElement TryItYourselfLinkCSS = wait.Until<IWebElement>((d) => { return driver.FindElement(By.CssSelector("div[class='w3-example']>a[href='tryit.asp?filename=trycss_default']")); });
        TryItYourselfLinkCSS.Click();

        Thread.Sleep(5000); //Static Wait is not recommended

        //Store all window handles in a list
        allWindowHandles = driver.WindowHandles.ToList();

        //Get new window handle, the one which is not equals to first two tabs.
        for (int i = 0; i < allWindowHandles.Count; i++)
        {
            int k = 0;

            for (int j = 0; j < allCapturedWindowHandles.Count; j++)
            {
                if (allWindowHandles[i] == allCapturedWindowHandles[j])
                {
                    k++;
                }
            }

            if (k == 0)
            {
                allCapturedWindowHandles.Add(allWindowHandles[i]);
            }
        }

        //Switch to new window i.e. third window
        driver.SwitchTo().Window(allCapturedWindowHandles[2]);

        Thread.Sleep(6000); //Static Wait is not recommended
        Console.WriteLine("\nSwitched to Third Tab. \nThird Tab Title: " + driver.Title);
        Console.WriteLine("Third Tab URL: " + driver.Url);

        //Switch to second tab
        driver.SwitchTo().Window(allCapturedWindowHandles[1]);

        Thread.Sleep(5000); //Static Wait is not recommended
        Console.WriteLine("\nSwitched to Second Tab. \nSecond Tab Title: " + driver.Title);
        Console.WriteLine("Second Tab URL: " + driver.Url);

        Console.WriteLine("\nTotal Tabs: " + allCapturedWindowHandles.Count);
        Console.WriteLine("All Window Handles: ");
        for (int i = 0; i < allCapturedWindowHandles.Count; i++)
        {
            Console.WriteLine(allCapturedWindowHandles[i]);
        }
    }

希望这会有所帮助:)