有没有办法让 Appium 可以专注于 Safari 中的新 window 选项卡?

Is there a way that Appium can focus to new window tab in Safari?

我正在执行我的任务,使用 Appium 自动执行特定网站的性能。我正在测试 iPadOS(iOS 14.x 及更高版本),单击按钮时该网站有一个新的选项卡功能。现在,问题是当我尝试使用 getWindowHandles 时什么也没有发生。这是我的代码:

String url = "https://demoqa.com/browser-windows"; driver.get(url);

    try {
        System.out.println(1321);
        Thread.sleep(5000);
        System.out.println(driver.getTitle()); 
        String currentHandle = driver.getWindowHandle(); 
        
        System.out.println(driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).isDisplayed());
        
        driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).click();
        System.out.println(driver.getTitle()); 
        Thread.sleep(5000);

        System.out.println(driver.getTitle());
        Thread.sleep(5000);
         //getting all the handles currently available
        Set<String> handles=driver.getWindowHandles();
        Thread.sleep(5000);
        System.out.println(handles.size());
        for(String actual: handles)
        {
            
         if(!actual.equalsIgnoreCase(currentHandle))
         {
             //switching to the opened tab
             driver.switchTo().window(actual); 
         }
        }

输出: 1321 工具QA 真的 工具QA 工具QA 1

你这样做的那一刻:

driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).click();

您需要切换到新版 tab/window

代码:

ArrayList<String> handles = new ArrayList<String> (driver.getWindowHandles());
System.out.println(driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).isDisplayed());
driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).click();
driver.switchTo().window(handles.get(1));
System.out.println(driver.getTitle()); 

更新 1:

driver.get("https://demoqa.com/browser-windows");
WebDriverWait wait = new WebDriverWait(driver, 10);
String currentHandle = driver.getWindowHandle();
System.out.println(driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).isDisplayed());
driver.findElement(By.xpath("//*[@id=\"tabButton\"]")).click();
Thread.sleep(2000);
ArrayList<String> handles = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(handles.get(1));
System.out.println("Switched");
System.out.println(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("sampleHeading"))).getText());