本文介绍了getWindowHandles() 在 Firefox 中不起作用 58.焦点仍然在父选项卡上,不会转移到下一个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习如何使用 Selenium 和 Java 在浏览器中处理多个选项卡.看起来我下面的代码不起作用.

I am starting to learn how to handle multiple tabs in a browser using Selenium with Java. looks like my code below is not working.

    import java.util.ArrayList;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class HandlingWindows {

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

        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        String parent= driver.getWindowHandle();
        System.out.println("Parent Window is"+parent);

        //Get Data Policy
        WebElement we= driver.findElement(By.linkText("Data Policy"));
        //Click Data Policy link
        we.click();
        //Create an arrayList
        ArrayList<String> s1= new ArrayList<String>(driver.getWindowHandles());


         for(String s2:s1)
           {


            if(!(s2.equalsIgnoreCase(parent)))
              {
               driver.switchTo().window(s2);
               Thread.sleep(5000);
               System.out.println(driver.getWindowHandle());
               System.out.println("get title of window"+driver.getTitle());
              }
             }
    }

}

请让我知道如何在不使用的情况下显示标题数据政策"获取窗口句柄().

Please let me know how can I display the title 'Data Policy' without usinggetWindowHandles().

推荐答案

getWindowHandles() 工作得很好,但在调用 getWindowHandles() 之前,你必须引入 WebDriverwait 如下:

getWindowHandles() works pretty fine but before invoking getWindowHandles() you have to induce WebDriverwait as follows :

System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.facebook.com/");
String parent= driver.getWindowHandle();
System.out.println("Parent Window is"+parent);
driver.findElement(By.linkText("Data Policy")).click();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> s1= driver.getWindowHandles();
for(String s2:s1)
{
    if(!parent.equalsIgnoreCase(s2))
        {
            driver.switchTo().window(s2);
            Thread.sleep(5000);
            System.out.println(driver.getWindowHandle());
            System.out.println("get title of window"+driver.getTitle());
        }
}

控制台输出:

Parent Window is4294967297
4294967303
get title of windowData Policy

这篇关于getWindowHandles() 在 Firefox 中不起作用 58.焦点仍然在父选项卡上,不会转移到下一个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 06:45