org.openqa.selenium.WebDriverException:无效参数:在Linux中使用Selenium和Java进行窗口处理时,"handle"必须为字符串 [英] org.openqa.selenium.WebDriverException: invalid argument: 'handle' must be a string while window handling with Selenium and Java in Linux

查看:378
本文介绍了org.openqa.selenium.WebDriverException:无效参数:在Linux中使用Selenium和Java进行窗口处理时,"handle"必须为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在CI管道中运行测试用例. VM是linux的地方. Selenium多窗口处理-switchTo()方法在linux平台上引发异常.

I have an requirement of running test cases in CI pipeline. where the VM is linux. Selenium multiple window handling - switchTo() method throws exception for linux platform.

例外:

org.openqa.selenium.WebDriverException: invalid argument: 'handle' must be a string

代码试用:

driver.switchTo().window(subWindowHandler);

按照多个窗口句柄方式声明:

Its declared as per multiple window handle way:

String subWindowHandler = null; 
Set<String> handles = driver.getWindowHandles(); 
Iterator<String> iterator = handles.iterator(); 
while (iterator.hasNext()) { 
    subWindowHandler = iterator.next(); 
}

此代码在本地Windows系统中完美运行.

This code works perfectly in local windows system.

推荐答案

此错误消息...

org.openqa.selenium.WebDriverException: invalid argument: 'handle' must be a string

...表示作为参数传递的句柄必须是字符串.

...implies that the handle which was passed as an argument needs to be a string.

从逻辑上讲,您已经很接近了.甚至在创建/识别第二个窗口句柄之前,driver.getWindowHandles()可能执行得还为时过早.

Logically you are pretty close. Possibly the driver.getWindowHandles() is getting executed too early even before the second window handle is getting created/recognized.

作为解决方案,您需要为numberOfWindowsToBe(2)引入 WebDriverWait ,并且可以使用以下代码块:

As a solution you need to induce WebDriverWait for numberOfWindowsToBe(2) and you can use the following code block:

String mainWindowHandler = driver.getWindowHandle(); // store mainWindowHandler for future references
//line of code that opens a new TAB / Window
new WebDriverWait(driver, 5).until(ExpectedConditions.numberOfWindowsToBe(2));  //induce WebDriverWait
Set<String> handles = driver.getWindowHandles(); 
Iterator<String> iterator = handles.iterator(); 
while (iterator.hasNext()) 
{ 
    String subWindowHandler = iterator.next(); 
    if (!mainWindowHandler.equalsIgnoreCase(subWindowHandler))
    {
        driver.switchTo().window(subWindowHandler);
    }
}

您可以在 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆