我想自动化一些报告。单击某个元素时,它将打开弹出对话框窗口,但我不会从中获取访问元素。因为,代码将在父窗口运行,它无法进入新打开的弹出对话框窗口,当我关闭浏览器时出现错误,但我不会在控制台中收到任何消息。
我使用了以下代码:
driver.findElement(By.id("gvWS_ctl01_ImageButton1")).click(); \\after click on this button, I won't get an access an element of popup
dialog window and code will be running at this statement only
driver.switchTo().frame("Weather Upload");
driver.switchTo().activeElement();
driver.findElement(By.id("WeatherUploadData1_btnUpload")).click();
答案 0 :(得分:0)
你可以使用提醒功能访问弹出窗口,下面是示例代码如何操作。
Alert prompt = driver.switchTo().alert();
// Get message in Alert pop up
message = prompt.getText();
System.out.println("Prompt pop up message :- " + message);
// Force the program to wait to view the dialog box
try{Thread.sleep(4000);}catch(Exception e){System.out.println(e.toString());}
// Type value in prompt dialog box
prompt.sendKeys("Akshay Kumar");
// Force the program to wait to view the dialog box
try{Thread.sleep(4000);}catch(Exception e){System.out.println(e.toString());}
// Click on Ok button
prompt.accept();
如果有效,请告诉我!
答案 1 :(得分:0)
在从弹出窗口
中选择任何元素之前,您需要切换到弹出窗口String parentWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> iterator = windowHandles.iterator();
while (iterator.hasNext()) {
String handle = iterator.next();
if (!handle.contains(parentWindow)) {
// Switch to popup and close it
driver.switchTo().window(handle);
// Perform required action in popup
}
}
// Switching back to parent window
driver.switchTo().window(parentWindow);