package demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import selenium.Browser;
public class TaoBao {
private String username = null;
private String pwd = null;
/**模拟登陆
* @param username
* @param pwd
* @param driver
* @return
*/
public void search(){
Browser.getProfile("59.40.165.102 8118");
WebDriver webDriver = Browser.getSingleton();
webDriver.get("https://www.taobao.com/");
Browser.waitForLoad();
if(username.length()>0){
login(webDriver,username,pwd);
}
query(webDriver, "手机");
webDriver.quit();
}
/**
* @param webDriver
*/
public void login(WebDriver webDriver,String username, String pwd) {
webDriver.findElement(By.cssSelector("div[class=site-nav-sign] a[href]")).click();
System.out.println(webDriver.getPageSource());
webDriver.findElement(By.id("J_Quick2Static")).click();
Browser.waitForLoad();
webDriver.findElement(By.id("TPL_username_1")).clear();
webDriver.findElement(By.id("TPL_username_1")).sendKeys(username);
//输入密码
webDriver.findElement(By.id("TPL_password_1")).clear();
webDriver.findElement(By.id("TPL_password_1")).sendKeys(pwd);
//点击登录按钮
webDriver.findElement(By.id("J_SubmitStatic")).click();
webDriver.switchTo().defaultContent();
Browser.waitForLoad();
}
public void query(WebDriver webDriver,String word){
webDriver.findElement(By.name("q")).sendKeys(word);
webDriver.findElement(By.cssSelector("button[class=btn-search]")).click();
Browser.waitForLoad();
System.out.println(webDriver.getPageSource());
}
public static void main(String[] args) {
TaoBao taobao = new TaoBao();
taobao.username = "";
taobao.pwd = "";
taobao.search();
}
}
浏览器类
package selenium;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import conf.SysConfigure;
import utils.SleepUtil;
public class Browser {
private static WebDriver driver = null;
private static FirefoxProfile profile;
/**
* 获取Web驱动器单例
*
* @return
*/
public static WebDriver getSingleton() {
if (profile == null) {
profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", SysConfigure.FireFoxDownloadDir);
// browser.download.folderList设置Firefox的默认下载文件夹。0是桌面;1是“我的下载”;2是自定义
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream, application/vnd.ms-excel, text/csv, application/zip");
}
if (driver == null) {
System.setProperty("webdriver.firefox.bin", SysConfigure.webdriverFirefoxBin);
driver = new FirefoxDriver(profile);
}
return driver;
}
public static void getProfile(String proxyStr) {
String[] iPPort = proxyStr.split("\\s+");
String proxyIp = iPPort[0];
String proxyPort = iPPort[1];
profile = new FirefoxProfile();
// 使用代理
profile.setPreference("network.proxy.type", 1);
// http协议代理配置
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
profile.setPreference("network.proxy.ssl", proxyIp);
profile.setPreference("network.proxy.ssl_port", proxyPort);
// 所有协议公用一种代理配置,如果单独配置,这项设置为false
profile.setPreference("network.proxy.share_proxy_settings", true);
}
/**
* 等待驱动器加载页面完成
*/
public static void waitForLoad() {
ExpectedCondition pageLoad = new ExpectedCondition() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, SysConfigure.WebDriverWaitTimeOutInSeconds);
wait.until(pageLoad);
SleepUtil.sleepRandom();
// System.out.println("加载完成");
}
/**
* 根据选择器判断元素是否存在
*
* @param Locator
* @return
*/
public static Boolean elementIsExist(By Locator) {
try {
driver.findElement(Locator);
return true;
} catch (org.openqa.selenium.NoSuchElementException ex) {
return false;
}
}
/**
* 根据标题切换窗口
*
* @param windowTitle
* @return
*/
public static boolean switchToWindow(String windowTitle) {
boolean flag = false;
try {
String currentHandle = driver.getWindowHandle();
Set handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (driver.getTitle().contains(windowTitle)) {
flag = true;
System.out.println("Switch to window: " + windowTitle + " successfully!");
break;
} else
continue;
}
}
} catch (NoSuchWindowException e) {
System.out.println("Window: " + windowTitle + " cound not found!" + e.fillInStackTrace());
flag = false;
}
return flag;
}
/**
* 根据标题关闭窗口
*
* @param title
* @return
*/
public static boolean closeOtherWindowByTitle(String title) {
boolean flag = false;
try {
String currentHandle = driver.getWindowHandle();
Set handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (driver.getTitle().contains(title)) {
flag = true;
driver.close();
break;
} else
continue;
}
}
driver.switchTo().window(currentHandle);
} catch (NoSuchWindowException e) {
System.out.println("Window: " + title + " cound not found!" + e.fillInStackTrace());
flag = false;
}
return flag;
}
/**
* 通过选择器找到元素并点击
*
* @param selector
*/
public static boolean clickBySelector(String selector) {
WebElement we = driver.findElement(By.cssSelector(selector));
try {
if (we != null) {
we.click();
waitForLoad();
return true;
}
} catch (NoSuchElementException e) {
System.out.println("当前页面:" + driver.getCurrentUrl());
System.out.println("元素没有找到" + selector);
return false;
}
return false;
}
/**
* 通过选择器找到输入框,并写入词语,模拟人为输入,一个一个字输入
*
* @param selector
* @param word
* @param isSubmit
*/
public static void inputAndSubmit(String selector, String word, boolean isSubmit) {
WebElement we = driver.findElement(By.cssSelector(selector));
if (we != null) {
we.clear();
we.sendKeys(word);
if (isSubmit)
we.submit();
} else {
System.out.println("当前页面:" + driver.getCurrentUrl());
System.out.println("元素没有找到" + selector);
System.exit(0);
}
}
/**
* 关闭Web驱动器
*/
public static void close() {
if (driver != null) {
driver.close();
// driver.quit();
driver = null;
}
}
}