Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.chrome.ChromeDriver;
/**
* This program demonstrates: with selenium, the process of launching the Chrome browser and logging into the QQ mailbox
*
* @author Lizeyang
*
*/
public class Seleniumutil
{
The path of the browser program
Private String Webbrowserpath = "";
Private Webdriver driver = null;
Private String loginurl = "https://mail.qq.com/cgi-bin/loginpage";//Login page of QQ mailbox
Private String username = "10000"; QQ Account
Private String Password = "HelloWorld"; QQ Password
Initializes the selenium to invoke and launches the browser
public void Init ()
{
Webbrowserpath = "D:/webdriver/chromedriver.exe";
System. SetProperty ("Webdriver.chrome.driver", Webbrowserpath);
Driver = new Chromedriver ();
}
Close browser
public void Quitdrvier ()
{
if (null! = driver)
{
Driver. Quit ();
}
}
public void Waitforsecond ()
{
Try
{
Thread. Sleep (1000);
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
Simulating the logon process
public void Login ()
{
Driver. Get (loginurl); Enter the QQ login page
webelement element = driver. Findelement (by.id ("UIn"));//find DOM element with ID named UIn
Element.sendkeys (username);//The current element is the UIn input box, fill in the user name into this input box
Waitforsecond (); Let the process wait for a while to avoid occasional elements getting an exception
element = driver. Findelement (By.id ("P"));//find DOM element with ID named P
Element.sendkeys (password);//fill in the password
Waitforsecond ();
element = driver. Findelement (by.id ("Btlogin"));//Find Login button
Waitforsecond ();
Element.click (); Click the login button
}
public void Demo ()
{
Init ();
Login ();
Quitdrvier ();
}
public static void Main (string[] args)
{
Seleniumutil util = new Seleniumutil ();
Util.demo ();
}
}
Selenium super simple Get Started "turn"