无法使用 Java 中的网络驱动程序在 Chrome 中切换到'current active tab'



我无法使用WebDriver切换到Chrome中的"当前活动标签"。我尝试了以下方法:
我们可以切换到选项卡,但是我们不知道哪个是当前活动选项卡

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

但是,获取当前活动选项卡句柄是问题所在。

已更新-

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

        [Test]
    public void MultipleWindows()
    {
        IWebDriver driver = new ChromeDriver(@"G:SeleniumToolsSelenium");
        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]);
        }
    }

希望这对:)有所帮助

最新更新