Selenium handles Ajix and documents the solution that failed to locate the element that was caused by not loading

Source: Internet
Author: User

First, the solution of ideas

Our general approach is to sleep time and to wait for an element to appear and then perform the related actions we need. But both of these approaches have obvious drawbacks.

The first way to sleep fixed time, the disadvantage is: 1. Waste time, sometimes the element is loaded OK, but still have to wait for sleep time to finish before execution; 2. Unstable, more difficult to weigh a more appropriate wait time, sometimes the server side is slow, causing the timeout element has not yet loaded (some elements are returned by the server), An exception to the element timeout is thrown at this point. 3. The script is redundant and inflexible.

The second way through the wait until function, add a condition to display the wait, if found to return directly, time-out did not find and then throw an exception, this method is more efficient than the first, but the efficiency of writing scripts is significantly reduced, the code complexity becomes higher.

Cons: 1. Script writing is inefficient because the amount of code that needs to be written increases and the complexity becomes higher. 2. Script redundancy, complexity becomes higher.

In today's Web applications, a large number of AJAX requests are generally used to implement local refreshes. Because the page is not refreshed, the selenium executes faster and often causes the exception to be thrown because the element is not loaded. Therefore, it is best to get the active state of Ajax and the active state of the document through JS before each operation, and then proceed to the next step if all the loading is completed. Get AJAX activity state can use $.active to judge, if $.active is 0, indicating that the Ajax activity is complete, through document.readystate to get the page request is complete, if equal to completed, indicating that the request completes. 2 judgments are true at the same time, proceed to the next steps:

 PublicBoolean pageLoaded () {javascriptexector JSE=(javascriptexector) driver; String JsCode1= "return $.active;"; String JsCode2= "return document.readystate;"; LongTime = Systen.currtime;//Get current Time    LongTimeout = 5;//setting the time-out period     while((System.currtime-time) <=timeout*1000){          if((Jse.execjs (jsCode1) ==0) && (Jse.execjs (jsCode2) = = "complete") {              return true; }} System.out.printf ("Page load has timed out!" "); return false;}

Selenium handles Ajix and documents the solution that failed to locate the element that was caused by not loading

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.