selenium-java-2.42.2.zip:http://download.csdn.net/detail/zzyo_mz/9507650
selenium-server-standalon-2.42.2.jar:http://download.csdn.net/detail/zzyo_mz/9507649
chrome.java(WebDriver)
package selerc;
import java.io.IOException;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class chrome {
public static void main(String[] args) throws IOException {
System.setProperty(
"webdriver.chrome.driver",
"D:/Eclipse/jar包/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
driver.findElement(By.xpath("//*[@id='u1']/a[7]")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement username = driver.findElement(By.id("TANGRAM__PSP_8__userName"));
username.sendKeys("百度账号");
WebElement password = driver.findElement(By.id("TANGRAM__PSP_8__password"));
password.sendKeys("百度密码");
WebElement btn = driver.findElement(By.id("TANGRAM__PSP_8__submit"));
btn.click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("华中科技大学软件学院");
driver.findElement(By.xpath("//*[@id='su']")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//*[@id='1']/h3/a[1]")).click();
System.out.println("Page title is: " + driver.getTitle());
//获取当前浏览器窗口标识
String currentHandle=driver.getWindowHandle();
//获取所有浏览器窗口标识
Set<String> handles=driver.getWindowHandles();
for(String handle:handles){
if(handle.equals(currentHandle))
continue;
else
driver.switchTo().window(handle);
}
System.out.println(driver.getWindowHandles());
//driver.close();
//System.out.println("Page title is: " + driver.getTitle());
}
}
demo.java(RC)
package selerc;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class demo {
public static void main(String[] args) throws InterruptedException
{
//Init the RC Server
Selenium selenium = new DefaultSelenium("localhost", 4444 , "firefox", "http://www.calculator.net/percent-calculator.html");
selenium.start(); // Start
selenium.open("https://www.baidu.com"); // Open the URL
selenium.windowMaximize();
Click on Link Math Calculator
//selenium.click("xpath=.//*[@id='menu']/div[3]/a");
//Thread.sleep(2500); // Wait for page load
//
Click on Link Percent Calculator
//selenium.click("xpath=.//*[@id='menu']/div[4]/div[3]/a");
//Thread.sleep(4000); // Wait for page load
// Focus on text Box
//selenium.focus("xpath=.//*[@id='kw']");
// enter a value in Text box 1
selenium.type("xpath=.//*[@id='kw']", "zzyo");
enter a value in Text box 2
//selenium.focus("xpath=.//*[@id='cpar2']");
//selenium.type("xpath=.//*[@id='cpar2']", "50");
// Click Calculate button
selenium.click("xpath=.//*[@id='su']");
// verify if the result is 5
String result = selenium.getText("xpath=.//*[@id='2']/h3/a");
selenium.stop();
System.out.println(result);
//Thread.sleep(10000);
//
//if (result == "5")
//{
//System.out.println("Pass");
//}else
//{
//System.out.println("Fail");
//}
}
}