本文介绍了如何在selenium webdriver中从一个弹出窗口切换到另一个弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方案是:


  1. 主窗口 - >做一些活动。

  2. 点击保存按钮 - >确认弹出,然后按OK和CANCEL按钮。

  3. 单击确认弹出窗口上的确定按钮 - >使用确定按钮打开另一个成功弹出窗口。

  4. 弹出成功后单击确定按钮。

  5. 切换到主窗口。

  1. main window-> do some activity.
  2. click on Save button-> Confirmation pop up open with OK and CANCEL button.
  3. Click on OK button on confirmation popup ->another success pop up open with OK button.
  4. click on OK button on success pop up.
  5. switch to main window.

PopUp以上是HTML弹出窗口。
如何处理硒中的上述情况?我是硒的新人。请帮助我。我坚持上述观点。

Above PopUp is HTML pop ups.How do i handle above scenario in selenium?. I am new on selenium. Please help me.I am stuck on above point.

代码

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第二次弹出

// When click on OK button of first popup the respective div is destroy and new div getting generated for Second pop up

<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>


推荐答案

点击保存按钮后,您应该处理这些信息对话框框如下: -

After click on Save button you should handle these information dialog box as below :-

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元素,因此您需要通过查找这些元素来正常处理它。

Note:- There is no need to switch window, these dialog box are simple HTML elements, So you need handle it normally by finding these elements.

Edited1 : - 如果您无法点击使用 WebElement.click()尝试使用 Actions 类在点击之前移动该元素,如下所示: -

Edited1 :- If you're unable to click using WebElement.click() try using Actions class to move that element before click as below :-

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 如下: -

Edited2:- If you're still unable to click try using JavascriptExecutor as below :-

//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);

这篇关于如何在selenium webdriver中从一个弹出窗口切换到另一个弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:07