I am learning selenium webdriver in the handling of alert, confirm, prompt encountered a tricky problem, is that the program executes too fast, resulting in no jump to the alert dialog box is executed, the result of 99% execution failure, and occasionally execute success. The following code is posted for your reference:
@Test
public void Testconfirmdialog () throws Exception
{
Driver.manage (). window (). Maximize ();
Webelement promptbtn = driver.findelement (By.xpath ("//input[@value = ' confirm ']");
Promptbtn.click ();
Alert jsconfirm = Driver.switchto (). alert ();
Jsconfirm.accept ();
Jsconfirm = Driver.switchto (). alert ();
Log.info (Jsconfirm.gettext ());
Jsconfirm.accept ();
Promptbtn.click ();
Jsconfirm = Driver.switchto (). alert ();
Jsconfirm.dismiss ();
Jsconfirm = Driver.switchto (). alert ();
Log.info (Jsconfirm.gettext ());
Jsconfirm.accept ();
}
The above code does not have any problems with single-step debugging, but it fails at run time and occasionally succeeds. Very depressed
Although the Internet is not recommended to use Thread.Sleep, but I did not find any other good way, that is, after clicking the button, add a Thread.Sleep (2000), let the program wait two seconds to execute.
@Test
public void Testconfirmdialog () throws Exception
{
Driver.manage (). window (). Maximize ();
Webelement promptbtn = driver.findelement (By.xpath ("//input[@value = ' confirm ']");
Promptbtn.click ();
try{
Thread.Sleep (Interval_time);
}catch (interruptedexception e)
{
E.printstacktrace ();
}
Alert jsconfirm = Driver.switchto (). alert ();
Jsconfirm.accept ();
try{
Thread.Sleep (Interval_time);
}catch (interruptedexception e)
{
E.printstacktrace ();
}
Jsconfirm = Driver.switchto (). alert ();
Log.info (Jsconfirm.gettext ());
Jsconfirm.accept ();
Promptbtn.click ();
try{
Thread.Sleep (Interval_time);
}catch (interruptedexception e)
{
E.printstacktrace ();
}
Jsconfirm = Driver.switchto (). alert ();
Jsconfirm.dismiss ();
try{
Thread.Sleep (Interval_time);
}catch (interruptedexception e)
{
E.printstacktrace ();
}
Jsconfirm = Driver.switchto (). alert ();
Log.info (Jsconfirm.gettext ());
Jsconfirm.accept ();
}
Selenium Webdriver How to handle problems with confirm dialog boxes