EN
Selenium java - basic full working example with selenium maven dependency
3 points
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:
xxxxxxxxxx
1
C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe
Change location of chrome driver or ensure you have it under above location.
Java selenium - read header name from wikipedia article.
Wikipedia article link:
xxxxxxxxxx
1
package com.dirask.read_header_name_from_wikipedia;
2
3
import org.openqa.selenium.By;
4
import org.openqa.selenium.WebDriver;
5
import org.openqa.selenium.WebElement;
6
import org.openqa.selenium.chrome.ChromeDriver;
7
import org.openqa.selenium.chrome.ChromeOptions;
8
9
public class Example {
10
11
public static void main(String[] args) {
12
ChromeOptions options = new ChromeOptions();
13
options.addArguments("--window-size=1366,768");
14
15
String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe";
16
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
17
WebDriver webDriver = new ChromeDriver(options);
18
19
try {
20
String filePath = "https://en.wikipedia.org/wiki/Java_(programming_language)";
21
webDriver.get(filePath);
22
23
WebElement element = webDriver.findElement(By.id("firstHeading"));
24
System.out.println(element.getText()); // Java (programming language)
25
} finally {
26
webDriver.close();
27
webDriver.quit();
28
}
29
}
30
}
Output
xxxxxxxxxx
1
Java (programming language)
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:
xxxxxxxxxx
1
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<title>Title</title>
6
</head>
7
<body>
8
9
<div id="test-id">Test</div>
10
11
</body>
12
</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.
xxxxxxxxxx
1
package com.dirask.read_html_file_from_disc_instead_of_url_website;
2
3
import org.openqa.selenium.By;
4
import org.openqa.selenium.WebDriver;
5
import org.openqa.selenium.WebElement;
6
import org.openqa.selenium.chrome.ChromeDriver;
7
import org.openqa.selenium.chrome.ChromeOptions;
8
9
/**
10
* If you run this example on windows put entire project in this dir:
11
* C:/projects/selenium_java_examples_by_dirask_com/
12
* or modify below absolute filePath
13
*/
14
public class Example {
15
16
public static void main(String[] args) {
17
ChromeOptions options = new ChromeOptions();
18
options.addArguments("--window-size=1366,768");
19
20
String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe";
21
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
22
WebDriver webDriver = new ChromeDriver(options);
23
24
try {
25
// modify location of the file, according to your project location
26
String filePath = "file:///" +
27
"C:/projects/selenium_java_examples_by_dirask_com/" +
28
"src/com/dirask/read_html_file_from_disc_instead_of_url_website/" +
29
"example.html";
30
webDriver.get(filePath);
31
32
WebElement element = webDriver.findElement(By.id("test-id"));
33
System.out.println(element.getText()); // Test
34
} finally {
35
webDriver.close();
36
webDriver.quit();
37
}
38
}
39
}
Output:
xxxxxxxxxx
1
Test
Download entire project:
Link to github repo here: