Selenium - Simple URL Ping

Use Selenium to create a program to

  • open chrome browser
  • open gmail.com
  • login

Download first:

  1. selenium-server-standalone-3.9.1.jar
  2. chromedriver_win32

UrlTestDemo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main;

import java.util.concurrent.TimeUnit;

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

public class UrlTestDemo {

static WebDriver driver;

public static void main(String[] args) {

String chromedriver = "D:\\workspace\\RyanTool\\lib\\chromedriver.exe";
String url = "https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin";
String userId = "xxx";
String password = "xxx";
String userIdXpath = "//*[@id=\"identifierId\"]";
String passwordXpath = "//*[@id=\"password\"]/div[1]/div/div[1]/input";
String idNextBtnXpath = "//*[@id=\"identifierNext\"]/content/span";
String pwdNextBtnXpath = "//*[@id=\"passwordNext\"]/content/span";

System.setProperty("webdriver.chrome.driver",chromedriver);
driver = new ChromeDriver();

openUrl(url);
input(userIdXpath, userId);
click(idNextBtnXpath);
input(passwordXpath, password);
click(pwdNextBtnXpath);
sleep(4);

driver.close();
}

private static void sleep(int i) {
try {
TimeUnit.SECONDS.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static void click(String btnXpath) {
driver.findElement(By.xpath(btnXpath)).click();
sleep(1);
}

private static void input(String xpath, String content) {
driver.findElement(By.xpath(xpath)).sendKeys(content);
sleep(1);
}

private static void openUrl(String url) {
driver.get(url);
sleep(3);
}

}