Selenium Java Example
In the following example, we present an use case where we use Selenium to open a web page using specified URL, enter a string in the search box, and click on a button.
This use case is kind of a standard example when we get started with Selenium.
Java Program
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.time.Duration; public class MyAppTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://google.com/ncr"); String title = driver.getTitle(); System.out.println("Title : " + title); driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); WebElement searchBox = driver.findElement(By.name("q")); WebElement searchButton = driver.findElement(By.name("btnK")); searchBox.sendKeys("Selenium"); searchButton.click(); searchBox = driver.findElement(By.name("q")); String value = searchBox.getAttribute("value"); System.out.println("Search value : " + value); driver.quit(); } }