Google Chrome Driver: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://sites.google.com/chromium.org/driver/
* https://sites.google.com/chromium.org/driver/getting-started
=Internal=
=Internal=
* [[Selenium_Concepts#Google_Chrome_Driver|Selenium Concepts]]
* [[Selenium_Concepts#Google_Chrome_Driver|Selenium Concepts]]
=Overview=
ChromeDriver is a separate executable that Selenium [[Selenium_Concepts#WebDriver|WebDriver]] uses to control [[Chrome]].
The executable is <code>chromedriver</code>. ChromeDriver expects you to have Chrome installed in the default location for your platform. ChromeDriver can be forced to use a custom location, by setting a special [[#Driver_Capabilities|capability]].
=Controlling a Chrome Browser from Java=
==Collocated Driver==
A Java program can control a browser via a collocated Chrome driver, which is started as the program runs:
<syntaxhighlight lang='java'>
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Main {
  public static void main(String[] args) throws Exception {
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.google.com/");
    WebElement searchBox = driver.findElement(By.name("q"));
    searchBox.sendKeys("ChromeDriver");
    searchBox.submit();
    driver.quit();
  }
}
</syntaxhighlight>
Assuming <code>chromedriver</code> is installed at the specified location and Chrome is installed in the default location on the system, the program will start the driver, then a Chrome browser and will execute the automation.
Playground project: {{External|https://github.com/ovidiuf/playground/tree/master/selenium/chromedriver/chromedriver-java}}
==RemoteWebDriver==
A Java program can also control a browser via a remote Chrome driver, accessible over network. The program will use the <code>RemoteWebDriver</code> API.
The remote Chrome driver should be started in advance on the test system:
<syntaxhighlight lang='bash'>
chromedriver
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 9515
Only local connections are allowed.
ChromeDriver was started successfully.
</syntaxhighlight>
The browser can be driven as follows:
<syntaxhighlight lang='java'>
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class Main {
  public static void main(String[] args) throws Exception {
    WebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), new ChromeOptions());
    remoteWebDriver.get("http://www.google.com/");
    WebElement searchBox = remoteWebDriver.findElement(By.name("q"));
    searchBox.sendKeys("ChromeDriver");
    searchBox.submit();
    remoteWebDriver.quit();
  }
}
</syntaxhighlight>
Playground project: {{External|https://github.com/ovidiuf/playground/tree/master/selenium/chromedriver/remote-chromedriver-java}}
=Driver Capabilities=
{{External|https://sites.google.com/chromium.org/driver/capabilities}}
=Operations=
==Installation==
===Mac===
<syntaxhighlight lang='bash'>
brew install --cask chromedriver
brew upgrade --cask chromedriver
</syntaxhighlight>
==Version==
<syntaxhighlight lang='bash'>
chromedriver --version
</syntaxhighlight>

Latest revision as of 08:03, 2 June 2021

External

Internal

Overview

ChromeDriver is a separate executable that Selenium WebDriver uses to control Chrome. The executable is chromedriver. ChromeDriver expects you to have Chrome installed in the default location for your platform. ChromeDriver can be forced to use a custom location, by setting a special capability.

Controlling a Chrome Browser from Java

Collocated Driver

A Java program can control a browser via a collocated Chrome driver, which is started as the program runs:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Main {
  public static void main(String[] args) throws Exception {
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.google.com/");
    WebElement searchBox = driver.findElement(By.name("q"));
    searchBox.sendKeys("ChromeDriver");
    searchBox.submit();
    driver.quit();
  }
}

Assuming chromedriver is installed at the specified location and Chrome is installed in the default location on the system, the program will start the driver, then a Chrome browser and will execute the automation.

Playground project:

https://github.com/ovidiuf/playground/tree/master/selenium/chromedriver/chromedriver-java

RemoteWebDriver

A Java program can also control a browser via a remote Chrome driver, accessible over network. The program will use the RemoteWebDriver API.

The remote Chrome driver should be started in advance on the test system:

chromedriver

Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 9515
Only local connections are allowed.
ChromeDriver was started successfully.

The browser can be driven as follows:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;

public class Main {
  public static void main(String[] args) throws Exception {
    WebDriver remoteWebDriver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), new ChromeOptions());
    remoteWebDriver.get("http://www.google.com/");
    WebElement searchBox = remoteWebDriver.findElement(By.name("q"));
    searchBox.sendKeys("ChromeDriver");
    searchBox.submit();
    remoteWebDriver.quit();
  }
}

Playground project:

https://github.com/ovidiuf/playground/tree/master/selenium/chromedriver/remote-chromedriver-java

Driver Capabilities

https://sites.google.com/chromium.org/driver/capabilities

Operations

Installation

Mac

brew install --cask chromedriver
brew upgrade --cask chromedriver

Version

chromedriver --version