[英]How to get notified when alert appeared in webpage using java with selenium >
I am trying to automate a website, but there is sudden appearance of alert box which is related some feed back which i need to close that alert then only i can proceed to new level in that site. 我正在尝试自动化一个网站,但突然出现的警报框与一些反馈相关,我需要关闭该警报,然后只有我可以进入该网站的新级别。 The problem is i am unable to find the alert box appears at particular time in that site. 问题是我无法在该网站的特定时间找到警报框。 it appears randomly. 它随机出现。 How to solve my problem. 如何解决我的问题。
The fastest solution I can think up is to create a method onAlert() that will get rid of the alert, something like 我能想到的最快的解决方案是创建一个方法onAlert() ,它将摆脱警报,类似于
try{
Alert alert = null;
try {
alert = driver.switchTo().alert();
} catch (Exception e) {
Robot robot = new Robot();
robot.keyPress(27);
robot.keyRelease(27);
}
if (alert != null) {
alert.dismiss();
}
}
After that, you just have to call it within your code every time. 之后,您只需每次都在代码中调用它。 I know this is not the best implementation, but it will work. 我知道这不是最好的实现,但它会起作用。
If you want to block unnecessary alert appearance during execution, you need to override alert function using JavascriptExecutor
as below :- 如果要在执行期间阻止不必要的警报外观,则需要使用JavascriptExecutor
覆盖警报功能,如下所示: -
((JavascriptExecutor)driver).executeScript("window.alert = function() { return true; }");
You should just run the above mentioned code once at every time when new page loading before doing other action. 您应该在每次加载新页面时运行上述代码一次,然后再执行其他操作。 It will override the alert functionally and accept it internally. 它将在功能上覆盖警报并在内部接受它。
Edited :- As you're saying it's a HTML
alert window instead of JavaScript
alert then above code wouldn't work, in this case you should hide this alert using JavascriptExecutor
once at every time when new page loading before doing other stuff if it is found as below : 编辑 : - 当你说它是一个HTML
警告窗口而不是JavaScript
警告时,上面的代码将无法工作,在这种情况下,你应该在每次加载新页面时使用JavascriptExecutor
隐藏此警报,然后再执行其他操作(如果是发现如下:
//find first alert element
List<WebElement> alert = driver.findElements(..);
if(alert.size() > 0 && alert.get(0).isDisplayed())
{
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'none'", alert.get(0));
}
Declare the global variable eg: 声明全局变量,例如:
public boolean acceptNextAlert = true;
Following is the function 以下是该功能
public String closeAlertAndGetItsText()
{
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
}
catch (NoAlertPresentException ex)
{
ex.printStackTrace();
}
finally
{
acceptNextAlert = true;
}
return StartUrl;
}
}
Call this function 调用此功能
String c=closeAlertAndGetItsText();
I found the solution based on Saurabh's code 我找到了基于Saurabh代码的解决方案
List<WebElement> alert = driver.findElements(..);
if(alert.size() > 0 && alert.get(0).isDisplayed())
{
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'none'", alert.get(0));
}
I have created class CloseAlert.java in my project and inside the class created a static method closeAlert. 我在我的项目中创建了类CloseAlert.java,并在类中创建了一个静态方法closeAlert。 created two list for the elements which are in appeared alert box and called this class in my main class when ever the new page is opened and it works fine now. 为出现警报框中的元素创建了两个列表,并在打开新页面时在我的主类中调用此类,现在它可以正常工作。
public class CloseAlert {
static List<WebElement> alert;
static List<WebElement> alert2;
public static void AlertClose(WebDriver driver)
{
alert = driver.findElements(By.xpath("//a[@class='fsrCloseBtn']"));
alert2 = driver.findElements(By.xpath("//div[@class='fsrDeclineButtonContainer']/a"));
if(alert.size() > 0 && alert.get(0).isDisplayed()||alert2.size() > 0 && alert2.get(0).isDisplayed())
{
if(driver.findElement(By.xpath("//div[@class='fsrDeclineButtonContainer']/a")).isDisplayed())
{
System.out.println("Alert found with no thanks button");
driver.findElement(By.xpath("//div[@class='fsrDeclineButtonContainer']/a")).click();
}
else if(driver.findElement(By.xpath("//a[@class='fsrCloseBtn']")).isDisplayed())
{
System.out.println("alert found with Close button" );
driver.findElement(By.xpath("//a[@class='fsrCloseBtn']")).click();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.