Use of Selenium WebDriver (2), seleniumwebdriver

Source: Internet
Author: User

Use of Selenium WebDriver (2), seleniumwebdriver

The get () method of WebDriver loads the page only in the current browser window and blocks the running of the program until the page is loaded (onload) or times out, you can set timeout when initializing an instance:

1 webDriver. manage (). timeouts (). pageLoadTimeout (10, TimeUnit. SECONDS); // set the timeout value to 10 SECONDS.

If you need to use a WebDriver instance to operate multiple browser windows at the same time, pay attention to the impact of this feature. By default, only one browser window is opened for a WebDriver instance, but multiple browser windows can be opened through the transit page or through javascript: pseudo protocol.

Due to the get blocking and javascript single-threaded execution requirements for concurrent testing, multithreading should be adopted.

Similar to get (), WebDriver is also used. the to () method of the Navigation interface, WebDriver. navigation also includes the back (), forward (), and refresh () methods to implement the browser's historical Navigation (back () and forward, do not receive step size parameters)

Selenium WebDriver can also be called by a cross-Server Browser through Selenium Standalone Server. For details, see the official documentation.

The WebDriver. Timeouts interface also has two other methods: setScriptTimeout () and implicitlyWait ():

You can use setScriptTimeout () to set the duration of waiting for an asynchronous script to be executed before an error is thrown.

The implicitlyWait () method can be used to set the waiting duration before the NoSuchElementException error is thrown when the element is searched. This is a recessive or global setting. It corresponds to the Explicit it Waits. For details, refer to explain.

The Timeout value needs to be properly set and optimized based on actual needs. If it is set to too short, a large number of Timeout errors will be thrown during the running of the program. If it is too long, the program efficiency will be reduced.

In addition, improper Timeout settings will generate unexpected waiting results during the program running. For example, both explicit and implicit Timeout periods are set, the timeout duration of the element to be searched is unknown.

 

If the page contains a frame, you need to use webDriver. switchTo (). frame (); to switch to the iframe before searching for the elements inside the frame.

1 webDriver.switchTo().frame(0);//switch to a frame by index

Frame () has three overload forms, which can be passed into the index number, frameElement, and name/handle string of the frame. You can use

1 webDriver.switchTo().defaultContent();

Or

1 webDriver.switchTo().parentFrame();

The difference between the two is that defaultContent () will switch to the first frame or top-level page; parentFrame () will switch to the parent-level frame.

The traversal of iframe includes nested iframe, which can be implemented recursively, for example:

1 public static void getFrame(){2         //do something3         iLen = webDriver.findElements(By.cssSelector("body iframe")).size(); 4         for (i=0;i<iLen;i++) {5             webDriver.switchTo().frame(i);6             getFrame();7             webDriver.switchTo().parentFrame();8         };9 }

Can iframe be embedded into your own page? The answer is yes, but it will not be embedded without limit. It will only embed three layers, and then the third-layer frame will not be embedded in the interior.

Some websites use the automatic jump Method to handle the 404 error. When a page is embedded into the target page through frame, if the page does not exist, the page is nested, when Selenium WebDriver is used to search for such iframe content, if the frame traversal policy is incorrect, an endless loop of frame traversal will occur or the program throws NoSuchFrameException.

Version 2.21 chromedriver uses webDriver. when getCurrentUrl () is used to obtain the url of a page, unlike the old version, after switchTo () arrives at a frame, the url of the frame is not returned, and the url of the top-level page is always returned. It is not 100% reliable to identify the frame page by obtaining the src attribute of the frame, because the page content of the frame can be dynamically written.

If you need to control the depth of recursive traversal when traversing the frame, you can set a depth threshold, which is returned when this threshold is reached during recursion, for example:

 1 static int fDepth = 2; 2 public static void getFrame() { 3     //do something 4     if (fDepth>0) { 5         iLen = webDriver.findElements(By.cssSelector("body iframe")).size();  6         for (int i=0;i<iLen;i++) { 7             fDepth--; 8             webDriver.switchTo().frame(i); 9             getFrame();10             fDepth++;11             webDriver.switchTo().parentFrame();12         };13     };14 };

In actual use, Selenium WebDriver runs slowly. What should I do?

To be continued!

Original article, reprinted, please indicate the source.

 

Related Article

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.