Commonly used WebDriver API
Visit a website
package com.wb.api; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class WebDriverTest { public static void main(String[] args) { WebDriver driver; // 设置浏览器驱动环境变量 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\ChromeCore\\chromedriver.exe"); driver = new ChromeDriver(); //方法1 driver.get("http://www.baidu.com"); //方法2 driver.navigate().to("http://www.imooc.com"); } }
Click the browser's Back function
// access Baidu Web driver.get ( "http://www.baidu.com" ); // access Sogou web driver.navigate () to ( "http://www.sogou.com." ); Driver. the Navigate () the Back ();. // visit on the Baidu web access
Click the browser forward function
// access Baidu Web driver.get ( "http://www.baidu.com" ); // access Sogou web driver.navigate () to ( "http://www.sogou.com." ); Driver. the Navigate () the Back ();. // Baidu web access on a visit driver.navigate () Forward ();. // Jump to page Sogou
Refresh the current page
// refresh the page driver.navigate () refresh ().;
Operation of the browser window
// declare a Point object 100 represents two with respect to the top left corner (0,0) of the abscissa and ordinate Point = Point new new Point (100, 100 ); // declare a Dimension object 200 represents two browsers window length and width of the Dimension Dim = new new the Dimension (200 is, 200 is ); // setPosition method of indicating the position set on the browser screen coordinates (100, 100) Point object // in certain versions of browsers this method fails driver.manage () window () setPosition (Point);.. // the setSize method set indicates the size of the browser window (200 is, 200 is) driver.manage () window () the setSize (Dim)..; // getPosition method retrieves the browser screen position // in some browser versions in this method fails System.out.println (driver.manage () window () getPosition ()..); // representation method getSize Gets the size of the browser window System.out.println(driver.manage().window().getSize());
Gets the Title property page
// get the page of the Title String title = driver.getTitle (); System.out.println (title);
Source code of the page
// get the page source code String pageSrCode = driver.getPageSource (); System.out.println (pageSrCode);
Gets the URL of the current page
// get the page the URL of String pageURL = driver.getCurrentUrl (); System.out.println (pageURL);
Typing in the input box
// Get input element
WebElement searchInput = driver.findElement (By.id ( "kW" ));
// the specified content input searchInput.sendKeys ( "Hello World");
Clear the contents of the input box
// Get input element WebElement searchInput = driver.findElement (By.id ( "kW" )); searchInput.sendKeys ( "Hello World" ); the try { the Thread.sleep ( 2000 ); } the catch (InterruptedException E) { E .printStackTrace (); } // clear the content input box searchInput.clear ();
Click the button
// get the button element WebElement btn = driver.findElement (By.id ( "btn" )); // Click the button element btn.click ();
Double-click on an element
// Get input element WebElement inputEle = driver.findElement (By.id ( "kW" )); // declare objects Actions Actions Builder = new new Actions (Driver); // double-clicking the input box builder.doubleClick (inputEle) .build () .perform ();