Selenium 测百度

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");
//}
}
}


转载于:https://my.oschina.net/zzyo/blog/668567

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值