Use of Selenium Webdriver (iii)

Source: Internet
Author: User

Selenium Webdriver support to drive a large number of browsers, including PHANTOMJS, Htmlunitdriver and other non-interface browser, they are fast, but the compatibility is still a bit problematic, sometimes encountered the page JS execution error, in some applications, Page content extraction is not simple and convenient.

For a common desktop browser, compatibility is good, but the performance of the implementation of the interface browser is less than the worse. One is the need to open the entity's browser to load all the resources of the page and render the page, which will consume a lot of CPU and memory resources, in the multi-threaded concurrency is particularly obvious, and second, through the browser's API for page content lookup extraction (Selenium RC is implemented by JS injection), Performance is subject to browser restrictions. Then, when using a desktop browser for Web page Automation testing, content fetching, provide some methods and suggestions for high performance:

Remove unnecessary configuration of resource consumption, open close browser window as few as possible, close unnecessary window in time, and always keep the browser window minimized.

Webdriver The browser form can be implemented to specify screen coordinate location, form size, maximize, but cannot minimize browser window:

1 webdriver.manage (). Windows (). SetPosition (new point (0, 0));//Specify window coordinates 2 Webdriver.manage (). Windows (). SetSize (new Dimension (1280,800));//Specify Window size 3 Webdriver.manage (). window (). Maximize ();//Maximize Windows

If you are using Google Chrome, you can do so by adding command line flag:

1 options.addarguments ("--window-position=0,0");//Specify window coordinates 2 options.addarguments ("-- window-size=1280,800 ");//Specify Window size 3 options.addarguments ("-start-maximized ");//Maximize window 4 options.addarguments ("--start-fullscreen");//fullscreen

Similarly, this approach does not provide a way to minimize the window. (If you need to adjust the window, it is recommended to implement command line flag, one step when opening the browser, webdriver.manage after opening the browser window to perform the adjustment)

Minimizing the browser window requires a separate programming implementation. A quick way to implement a AutoIt script:

1$aWinList = Winlist ("[Regexptitle: (? i) (. *google chrome.*)] ")2For $i =1To $aWinList [0][0]3If Bitand (Wingetstate ($aWinList [$i] [1]),2) Then4Winsetstate ($aWinList [$i] [1],"", @SW_MINIMIZE)5Winsetstate ($aWinList [$i] [1],"", @SW_HIDE)6 EndIf7Next

By monitoring and closing pop-up windows, Google Chrome is turned on by default, but unreliable, and can be turned off by "--disable-popup-blocking".

Delete the remaining temporary files, processes, etc. that occupy the resource, and the code slightly.

Minimizing the browser window can reduce the CPU footprint of the browser, especially on large pages that contain a large number of animated elements.

Some pages that use lazy load or load content asynchronously, in order to obtain the page elements reliably, can be executed after the page load through the Action.clickandhold (), Js.executescript (), the way to perform the triggering action.

By looping through the element lookup, return the search results as soon as possible, reducing all operations on the webelement. Traversing the webelements returned by Findelements (), use foreach to always find from the highest node, and to reverse-traverse when it is more likely to hit the internal child node.

The use of XPath is slower when cssselector is preferred and the lookup range is minimized. (Google Chrome v49 measured also not slow how much!-_-)

How do I get request and response information?

Cond!

Original articles, reproduced please indicate the source.

Use of Selenium Webdriver (iii)

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.