Common methods for Web Automation encapsulation (occasional updates)

Source: Internet
Author: User

1. Click action for Browser Alert pop-up that may be delayed for some reason

Public void Waitalertclick () {
webdriverwait wait = new webdriverwait (driver, 10);//Maximum 10s
try {
Alert alert = Wait.until (new expectedcondition<alert> () {
@Override
Public Alert Apply (Webdriver driver1) {
try {
return Driver.switchto (). alert ();//Get Alert Object
} catch (Noalertpresentexception | Unhandledalertexception ignored) {//exceptions can be captured according to the actual situation
return null;
}
}
});
alert.accept ();//Click OK here, you can do other things
} catch (NullPointerException e) {
System.out.println ("No Alert");//wait timeout, no pop-up window, Alert null is executed here
}

2. For the display state of elements with hidden attributes, wait for the page load element to be visible, and then proceed to the next step
public void  Waiteleisdisplay (by) {//wait for elements to be visible, up to 15s.
webdriverwait wait = new webdriverwait (driver, 15);
try {
Wait.until (New expectedcondition<boolean> () {
@Override
Public Boolean apply (Webdriver driver1) {
try {
Webelement ele=driver.findelement (by);
return ele.isdisplayed ();
}catch (Nosuchelementexception e) {
return null;
}
}
});
}catch (TimeoutException e) {
System.out.println ("Wait for element display timeout timed out after seconds waiting for:" +by);
}

}


Common methods for Web Automation encapsulation (occasional updates)

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.