selenium operation ChromeDriver

The ChromeDriver driver is required to operate the chrome browser with selenium .
What is ChromeDriver?
ChromeDriver is developed and maintained by the Chromium team, and it is a separate service that implements the WebDriver wire protocol. ChromeDriver controls the browser through chrome's automatic proxy framework. ChromeDriver is only compatible with chrome browsers of version 12.0.712.0 and above.
So in order for selenium to successfully operate the chrome browser, you need to go through the following steps:
1. Download the ChromeDriver driver package (download address: http://chromedriver.storage.googleapis.com/index.html?path=2.7/

Pay attention to reading note.txt to download the driver package of the same version as the browser you are using.
2. Specify the location of the ChromeDriver, which can be specified in two ways:
1) By configuring the ChromeDriver.exe location to the path environment variable.
2) Implemented through the webdriver.chrome.driver. system property. The implementation code is as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe");
3. The last thing you need to do is to create a new instance of ChromeDriver.
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/"); 
At this point, we can execute our automation code through the chrome browser.
The complete example code is as follows:

public static void main(String[] args) {

           // TODO Auto-generated method stub

//Set the path to access ChromeDriver

System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\LocalSettings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe");

           WebDriver driver = new ChromeDriver();

           driver.get("http://www.baidu.com/");

 

}

btw:
Chrome browser default location in each system:
YOU Expected Location of Chrome
Linux /usr/bin/google-chrome1
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe


Execute the above code, you will find that ChromeDriver is only created or started, and the browser is closed when quit is called. ChromeDriver is a lightweight service. If it is frequently started and closed in a relatively large test suite, it will increase a relatively obvious delay. In order to avoid the situation that the browser process is not closed, we can control the life and death of the ChromeDriver process through ChromeDriverService, so as to achieve the effect of closing the process when it is used up to avoid the process occupancy (Running the  server  in a child process).
The specific implementation is as follows:
ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable(new File("E:\\Selenium WebDriver\\chromedriver_win_23.0.1240.0\\chromedriver.exe")).usingAnyFreePort().build();
service.start();
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.quit();
// 关闭 ChromeDriver 接口
service.stop();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325862012&siteId=291194637