selenium 是一个 web 的自动化测试工具
Selenium WebDriver在启动火狐,作为自动化测试工具,可以直接通过浏览器对页面按照之前设置的程序进行操作,因为是完完全全原生浏览器的操作,所以我们人工可以完成的工作原则上WebDriver都可以帮助我们完成,这就是他强大的地方。
准备工作下载
(1)最新火狐
(2)火狐需要驱动geckodriver.exe
/mozilla/geckodriver/releases/tag/v0.27.0
maven需要配置
org.seleniumhq.selenium
selenium-java
2.53.0
org.seleniumhq.selenium
selenium-htmlunit-driver
2.52.0
实测ok
package com.demomodel.utils.fuzhu.goShopping;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;
public class myThread extends Thread{
private String name;
public myThread(String name){
this.name = name;
}
public void run(){
// 小米官网抢购红米k30 至尊 /buy/detail?product_id=12605
///buy/detail?product_id=10000233
String baseurl = "/buy/detail?product_id=12605";
// 小米官网登录信息
String baseurl2 = "/pass/serviceLogin?callback=http%3A%2F%%2Flogin%2Fcallback%3Ffollowup%3Dhttp%253A%252F%%252Fbuyphone%252Fnote3%26sign%3DZmYyZmU3NmE0NWNmMjZkYzk5YmU5ZWJjMzc2Zjg1YzNhMjY3NTJjMg%2C%2C&sid=mi_eshop";
WebElement loginElement = null;
WebElement element = null;
List elements = null;
List taocan = null;
WebElement btn = null;
//webdriver.gecko.drive webdriver.firefox.marionette
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Mozilla Firefox\\geckodriver.exe");
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// capabilities.setCapability("marionette", true);
//
//下面的语句用来启动带插件的火狐浏览器
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("default");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
//创建一个叫driver的对象,启动火狐浏览器
WebDriver driver = new FirefoxDriver(options);
//WebDriver driver = new FirefoxDriver();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 设置隐式等待10秒钟
//最大化浏览器窗口
// driver.manage().window().maximize();
System.out.println("starting:");
driver.get(baseurl2);
driver.findElement(By.id("username")).sendKeys("18837137"); // 输入自己小米官网登录账号
driver.findElement(By.id("pwd")).sendKeys("w2213858"); // 输入小米官网登录密码
driver.findElement(By.id("login-button")).click();
System.out.println(driver.getCurrentUrl());
try {
sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
driver.navigate().to(baseurl);
System.out.println("hello world!!");
elements = driver.findElements(By.xpath("//*[@class=\"option-box\"][div=\"选择颜色\"]/ul/li"));
if(elements == null || elements.size() == 0){
driver.close();//关闭
}
System.err.println(elements.size()+"====>"+2);
elements.get(2).click(); // 选择 白0 极夜黑1 薄荷绿2
try {
sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
elements = driver.findElements(By.xpath("//*[@class=\"option-box\"][div=\"选择版本\"]/ul/li"));
System.err.println(elements.size()+"===>"+0);
elements.get(0).click();// 选择8GB+128GB
elements = driver.findElements(By.className("sale-btn"));
elements.get(0).click(); // 点击下一步抢购按钮 等待
//driver.quit(); //退出
System.out.println(elements.size());
}
}
import java.util.Iterator;
import java.util.Random;
public class test{
public static void main( String[] args ) throws InterruptedException{
//1个线程跑 10s完成登录 15s进入页
//5线程跑 提前1分钟完成登录
for (int i = 0; i < 5; i++) {
myThread th1 = new myThread("A"+System.currentTimeMillis());
th1.start();
}}}