Languages
[Edit]
EN

Selenium java - basic full working example with selenium maven dependency

3 points
Created by:
Marcin
17290

Overview

In this short article we would like to show you basic usage of selenium in java.

This article contains 2 examples:

  • Example 1 - java selenium - read header name from wikipedia article.
  • Example 2 - java selenium -┬áread HTML file from disc instead of website.

In below examples we hard coded (for simplicity reason) path to chrome driver:

C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe

Change location of chrome driver or ensure you have it under above location.

Example 1

Java selenium - read header name from wikipedia article.

Wikipedia article link:

package com.dirask.read_header_name_from_wikipedia;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Example {

    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--window-size=1366,768");

        String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        WebDriver webDriver = new ChromeDriver(options);

        try {
            String filePath = "https://en.wikipedia.org/wiki/Java_(programming_language)";
            webDriver.get(filePath);

            WebElement element = webDriver.findElement(By.id("firstHeading"));
            System.out.println(element.getText()); // Java (programming language)
        } finally {
            webDriver.close();
            webDriver.quit();
        }
    }
}

Output

Java (programming language)

Example 2

Selenium application to read HTML file from disc instead of website. Selenium WebDriver can read the website from URL or from our disc.

example.html file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="test-id">Test</div>

</body>
</html>

In below example I hard coded the lcoation of example.html.

Ensure the path to example.html file is correct. If the path is not correct we will get exception.

package com.dirask.read_html_file_from_disc_instead_of_url_website;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * If you run this example on windows put entire project in this dir:
 * C:/projects/selenium_java_examples_by_dirask_com/
 * or modify below absolute filePath
 */
public class Example {

    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--window-size=1366,768");

        String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        WebDriver webDriver = new ChromeDriver(options);

        try {
            // modify location of the file, according to your project location
            String filePath = "file:///" +
                    "C:/projects/selenium_java_examples_by_dirask_com/" +
                    "src/com/dirask/read_html_file_from_disc_instead_of_url_website/" +
                    "example.html";
            webDriver.get(filePath);

            WebElement element = webDriver.findElement(By.id("test-id"));
            System.out.println(element.getText()); // Test
        } finally {
            webDriver.close();
            webDriver.quit();
        }
    }
}

Output:

Test

Github repository links

Download entire project:

Link to github repo here:

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join