Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium-java version – 4.4.0
chrome browser version – 105
chrome driver version – 105 (Download link : https://chromedriver.chromium.org/downloads) [Used WebDriverManager to auto download and used the expected version]
Chrome browser can be opened using selenium by following below steps:
1. Map the correct chromedriver using WebDriverManager,
WebDriverManager.chromedriver().setup();
2. Instantiate (creation of object) driver object with ChromeDriver() class (This step opens the chrome browser)
WebDriver driver = new ChromeDriver();
3. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected website can be carried out
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); driver.manage().window().maximize(); driver.get("https://www.google.com");
4. Quit/close the driver object after automating the necessary flows within the website.
driver.quit();
Sample code for reference
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class OpenChromeUsingSelenium4 {
public static void main(String[] args){
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.quit();
}
}