在切换上下文之前使用WebDriverWait方法等待web元素的存在,使用以下代码示例:
// 切换至WEBVIEW上下文前等待Web元素加载完成
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("some-id")));
// 切换到WEBVIEW上下文
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains("WEBVIEW")) {
driver.context(contextName);
break;
}
}
// 在WEBVIEW上下文中查找并操作Web元素
driver.findElement(By.id("some-id")).click();