我正在使用Selenium来测试网站。我可以上传一个“.txt”文件,然后双击它打开,但我无法使用selenium关闭打开的文件!
我知道有一个使用Alt + F4的机器人工具的解决方案,但我不允许使用机器人,我尝试使用下面的selenium代码来关闭窗口,它不起作用:
action.sendKeys(Keys.chord(Keys.ALT,Keys.F4)).build().perform();
答案 0 :(得分:1)
试试这个(WebDriver
是String myWindow = driver.getWindowHandle();
//open file with double click
//do something ...
driver.switchTo().window(myWindow);
的一个实例):
driver.quit();
这会存储原始窗口的句柄并切换回原来的窗口。如果您拨打// using ajax return to recreate a table
// ...
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
var $mytablerow = $(
'<table>' +
'<tbody>' +
'<tr>' +
'<td id="td' + data[i].Value + '" >' +
data[i].Text +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>'
);
}
}
var $mylst = $("#Userslist");
$mylst.html($mytablerow);
// ....
答案 1 :(得分:0)
感谢:CODEBLACK
String parentHandle = driver.getWindowHandle(); // get the current window handle
driver.findElement(By.xpath("//*[@id='someXpath']")).click(); // click some link that opens a new window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//code to do something on new window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
[1]: https://stackoverflow.com/questions/19112209/how-to-handle-the-new-window-in-selenium-webdriver