Use of Selenium WebDriver (1), seleniumwebdriver

Source: Internet
Author: User

Use of Selenium WebDriver (1), seleniumwebdriver

Introduction to Selenium WebDriver and resource download: http://docs.seleniumhq.org/

Selenium WebDriver was updated to version 2.52.0 in March February 2016. We recommend that you use the earlier version to upgrade to this version, which improves stability and performance.

Selenium supports various browsers, including PCs, mobile terminals, and PhantomJS.

Take the JAVA Development Environment in windows as an example to download. Google Chrome and PhantomJS are recommended for PCs. PhantomJS can be understood as Safari without interfaces (QtWebkit is used as the rendering engine, and the JS engine is JavascriptCore ). To enable Selenium to drive Google Chrome, you also need to download chromedriver: https://sites.google.com/a/chromium.org/chromedriver/downloadsaccording to your system environment. At the time of writing this article, the latest version of chromedriver is 2.21. Note that Version 2.21 supports Chrome as a v46-50 and the program may not work if chromedriver does not match the Chome version you are using.

After downloading and decompressing chromedriver, you can place it in any directory and specify the environment variable path in the program:

1 System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\driver\\chromedriver.exe");2 WebDriver webDriver = new ChromeDriver();3 webDriver.get("https://www.google.com/");

When you drive and start Google Chrome, the browser instance does not have any user environment configuration and needs to be loaded by yourself. For example:

Specify the cache directory of Google Chrome:

1 ChromeOptions options = new ChromeOptions();2 options.addArguments("--disk-cache-dir="+System.getProperty("user.dir")+"\\cache");3 WebDriver webDriver = new ChromeDriver(options);

Command line flag such:

-- Start-maximized: maximizes the browser window at startup;

-- Window-position = x, y: Specifies the screen coordinates of the browser at startup;

-- Window-size = w, h: Specify the browser width and height at startup;

-- Disk-cache-size = s: Specifies the disk cache size available for the browser;

-- Media-cache-size = 1: Specifies the media File cache size;

-- Ignore-certificate-errors: ignore certificate errors;

-- Disable-extensions: disable extension;

-- Disable-translate: disable translation conversion;

For more information, see: http://www.chromium.org/developers/how-tos/run-chromium-with-flags,http://peter.sh/experiments/chromium-command-line-switches/

Another way is to use capabilities, refer to: https://sites.google.com/a/chromium.org/chromedriver/capabilities

If you use phantomjs, specify the path of the system environment variable as follows:

1 System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "\\driver\\phantomjs.exe");2 webDriver = new PhantomJSDriver();3 webDriver.get("https://www.google.com/");

After get () is completed, you can operate the browser and extract the page content, for example:

Extract the image from the page:

1 WebElement webElement;2 List<WebElement> webElements;3 webElements = webDriver.findElements(By.cssSelector("body img"));4 for (webElement : webElements) {5     System.out.println(webElement.getAttribute("src"));6     System.out.println(webElement.getSize());7 };

Selenium WebDriver API reference address: http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html

If multiple browser windows are opened during get (), you can switch to the windows through switchTo (). window (winHandle:

 1 String defaultWinHandle = webDriver.getWindowHandle(); 2 if (webDriver.getWindowHandles().size()>1) { 3     for(String winHandle : webDriver.getWindowHandles()){ 4         if (!winHandle.equals(defaultWinHandle)) { 5             webDriver.switchTo().window(winHandle); 6             //do something... 7             //close window: webDriver.close(); 8         }; 9     };10     webDriver.switchTo().window(defaultWinHandle);11 };

After the operation is complete, exit:

1 webDriver.quit();

If iframe is available on the page, how can we cut in and out reliably? What if iframe is nested?

To be continued!

Original article, reprinted, please indicate the source.

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.