我的情景是:
以上PopUp是HTML弹出窗口。 我如何在硒中处理上述情况?我是硒的新人。请帮助我。我被困在上面。
代码
String ParentWindow = driver.getWindowHandle(); //switching from parent to pop up window
for (String Child_Window : driver.getWindowHandles()) {
driver.switchTo().window(Child_Window);
WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("test")));
driver.findElement(By.xpath("//input[@value='test']")).click();
}
driver.switchTo().window(ParentWindow);
HTML
<div>
<div class="msgBoxContainer">
<div id="msgBox1473308035532Image" class="msgBoxImage">
<img src="styles/images/confirm.png">
</div>
<div id="msgBox1473308035532Content" class="msgBoxContent">
<p>
<span>Saveでよろしいですか??</span>
</p>
</div>
</div>
<div id="msgBox1473308035532Buttons" class="msgBoxButtons">
<input id="msgBox1473308035532FirstButton" class="msgButton" type="button" value="はい" name="はい">
<input class="msgButton" type="button" value="いいえ" name="いいえ">
</div>
</div>
</div>
//当点击第一个弹出窗口的OK按钮时,相应的div将被销毁,并且为第二个弹出窗口生成新的div
<div id="msgBox1473308225709" class="msgBox" style="background-image: url("styles/images/msgBoxBackGround.png"); opacity: 1; top: 52.5px; left: 566.5px;">
<div class="msgBoxTitle">Information</div>
<div>
<div class="msgBoxContainer">
<div id="msgBox1473308225709Image" class="msgBoxImage">
<img src="styles/images/info.png">
</div>
<div id="msgBox1473308225709Content" class="msgBoxContent">
<p>
<span>登録完了</span>
</p>
</div>
</div>
<div id="msgBox1473308225709Buttons" class="msgBoxButtons">
<input id="msgBox1473308225709FirstButton" class="msgButton" type="button" value="はい" name="はい">
</div>
</div>
</div>
答案 0 :(得分:2)
单击“保存”按钮后,您应该处理以下信息对话框: -
WebDriverWait wait = new WebDriverWait(driver,10);
//For first dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();
//Now same as for second dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();
注意: - 不需要切换窗口,这些对话框是简单的HTML元素,所以你需要通过查找这些元素来正常处理它。
Edited1 : - 如果您无法使用WebElement.click()
点击,请尝试使用Actions
类在点击之前移动该元素,如下所示: -
Actions act = new Actions(driver);
//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(firstDialog).click().perform();
//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(secondDialog).click().perform();
Edited2 : - 如果您仍然无法点击,请尝试使用JavascriptExecutor
,如下所示: -
//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",firstDialog);
//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",secondDialog);
答案 1 :(得分:1)
1)主窗口 - &gt;做一些活动。
编写代码
2)点击“保存”按钮 - &gt;使用OK和CANCEL按钮弹出确认弹出。
确认大便
Alert simpleAlert = driver.switchTo().alert();
String alertText = simpleAlert.getText();
System.out.println("Alert text is " + alertText);
simpleAlert.accept();
3)点击确认弹出窗口上的确定按钮 - &gt;用OK按钮打开另一个成功弹出窗口。
使用确定按钮弹出另一个成功。
simpleAlert.accept();
4)弹出成功后点击OK按钮。
WebDriverWait wait = new WebDriverWait(driver, 30);
simpleAlert.accept();
5)切换到主窗口。
driver.switchTo().defaultContent();
或者您可以使用以下代码
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
// perform operations on popup
driver.switchTo().window(parentWindowHandler);
答案 2 :(得分:0)
由于弹出了java脚本,您可以使用警报类。
WebDriverWait wait = new WebDriverWait(driver, 30);
// With first Alert
Alert firstAlert = wait.until(ExpectedConditions.alertIsPresent());
firstAlert.accept();
// With Second alert
Alert secondAlert = wait.until(ExpectedConditions.alertIsPresent());
secondAlert.accept();
driver.switchTo().defaultContent();