Selenium Webdriver How to handle problems with confirm dialog boxes

Source: Internet
Author: User
Tags xpath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.