我已经打开链接,我点击该页面上的登录链接, 通过单击登录按钮,它会自动在新选项卡中打开。我必须在该标签中填写表格。为此,我尝试了以下代码:
emptyDS
通过这个我可以切换焦点但不能在此选项卡中执行任何操作。 请建议
答案 0 :(得分:2)
Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
for (String handle : handles) {
if (!handle .equals(currentHandle))
{
driver.switchTo().window(handle);
}
}
//fill your form in the other tab
......
//go back to first tab if you want
driver.switchTo().window(currentHandle);
答案 1 :(得分:1)
这是将硒与Node.js和Typescript一起使用时的代码
const mainHandle = await this.driver.getWindowHandle();
// load new tab here ..
const allHandles = await this.driver.getAllWindowHandles();
for (const handle of allHandles) {
if (handle !== mainHandle) {
await this.driver.switchTo().window(handle);
}
}