1. Prerequisites to run Selenium scripts on Chrome browser
Java JDK installed
Eclipse/IntelliJ IDEA/VS Code IDE
Chrome browser
ChromeDriver (compatible with your Chrome version)
Selenium library
From Selenium 4.6 onwards, you don't need to manually download ChromeDriver — Selenium Manager handles it automatically.
2. Maven Setup
If you’re using Maven, add this to your pom.xml:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.20.0</version> <!-- Use the latest --> </dependency> </dependencies>
3. Selenium Java Script for Chrome
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeTest { public static void main(String[] args) { // Initialize ChromeDriver (auto-managed by Selenium Manager from Selenium 4.6+) WebDriver driver = new ChromeDriver(); // Maximize the window driver.manage().window().maximize(); // Open a website driver.get("https://www.google.com"); // Print the title System.out.println("Page Title: " + driver.getTitle()); // Close the browser driver.quit(); } }
4. If Using Older Selenium (< 4.6)
Download ChromeDriver manually and set the path:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver();
No comments:
Post a Comment