119 lines
5.5 KiB
Java
119 lines
5.5 KiB
Java
import com.thoughtworks.selenium.Selenium;
|
|
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
|
|
import io.netty.handler.codec.http.HttpRequest;
|
|
import io.netty.handler.codec.http.HttpResponse;
|
|
import net.lightbody.bmp.BrowserMobProxy;
|
|
import net.lightbody.bmp.BrowserMobProxyServer;
|
|
import net.lightbody.bmp.client.ClientUtil;
|
|
import net.lightbody.bmp.core.har.Har;
|
|
import net.lightbody.bmp.filters.RequestFilter;
|
|
import net.lightbody.bmp.filters.ResponseFilter;
|
|
import net.lightbody.bmp.util.HttpMessageContents;
|
|
import net.lightbody.bmp.util.HttpMessageInfo;
|
|
import net.sf.json.JSONObject;
|
|
import org.junit.Test;
|
|
import org.openqa.selenium.*;
|
|
import org.openqa.selenium.Point;
|
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
import org.openqa.selenium.interactions.Actions;
|
|
import org.openqa.selenium.remote.CapabilityType;
|
|
import org.openqa.selenium.remote.DesiredCapabilities;
|
|
import org.openqa.selenium.remote.RemoteWebDriver;
|
|
import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
|
|
|
import java.awt.*;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* Created by xy on 2015/6/14.
|
|
*/
|
|
public class ChromeDriverTest {
|
|
|
|
|
|
// private void loadPage() throws MalformedURLException, InterruptedException {
|
|
//
|
|
// // start the proxy
|
|
// BrowserMobProxy proxy = new BrowserMobProxyServer();
|
|
// proxy.start(0);
|
|
// proxy.addResponseFilter(new ResponseFilter() {
|
|
// public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
|
|
// System.out.println(new JSONObject(httpMessageContents.getTextContents()));
|
|
// }
|
|
// });
|
|
// Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
|
|
// DesiredCapabilities capabilities = new DesiredCapabilities();
|
|
// capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
|
|
// System.setProperty("webdriver.chrome.driver", "D:\\Users\\wenyan\\IdeaProjects\\Selenium\\src\\main\\resources\\chromedriver.exe");
|
|
// WebDriver driver = new ChromeDriver();
|
|
// driver.get("http://map.baidu.com");
|
|
// JavascriptExecutor jse = (JavascriptExecutor)driver;
|
|
// jse.executeScript("document.getElementById('PoiSearch').value='灏忓尯'");
|
|
// Thread.sleep(10 * 1000);
|
|
// jse.executeScript("document.getElementById('poiSearchBtn').click()");
|
|
// try {
|
|
// Thread.sleep(20*1000);
|
|
// } catch (InterruptedException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
// driver.close();
|
|
// }
|
|
|
|
@Test
|
|
public void fun() throws IOException, InterruptedException, AWTException {
|
|
// System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\Selenium\\src\\main\\resources\\chromedriver.exe");
|
|
// start the proxy
|
|
// BrowserMobProxy proxy = new BrowserMobProxyServer();
|
|
// proxy.addResponseFilter(new ResponseFilter() {
|
|
// public void filterResponse(HttpResponse httpResponse, HttpMessageContents httpMessageContents, HttpMessageInfo httpMessageInfo) {
|
|
// if(httpMessageInfo.getOriginalUrl().startsWith("http://map.baidu.com/?newmap=1&")){
|
|
// System.out.println(JSONObject.fromObject(httpMessageContents.getTextContents()));
|
|
// }
|
|
// }
|
|
// });
|
|
// proxy.start(0);
|
|
// // get the Selenium proxy object
|
|
// Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
|
|
//
|
|
// // configure it as a desired capability
|
|
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
|
|
// capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
|
|
// start the browser up
|
|
FirefoxDriver driver = new FirefoxDriver();
|
|
// RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"),capabilities);
|
|
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
|
|
driver.manage().window().maximize();
|
|
// proxy.newHar("map.baidu.com");
|
|
driver.get("http://map.baidu.com");
|
|
driver.findElement(By.id("PoiSearch")).clear();
|
|
driver.findElement(By.id("PoiSearch")).sendKeys("灏忓尯");
|
|
driver.executeScript("document.getElementById('PoiSearch').focus()");
|
|
Thread.sleep(3000);
|
|
Actions click = new Actions(driver);
|
|
click.sendKeys(Keys.ARROW_DOWN);
|
|
click.sendKeys(Keys.ENTER);
|
|
click.perform();
|
|
Thread.sleep(3000);
|
|
while(true){
|
|
WebElement draggablePartOfScrollbar = null;
|
|
int i = 0;
|
|
while(draggablePartOfScrollbar == null && i < 3){
|
|
try{
|
|
i++;
|
|
draggablePartOfScrollbar = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.id("tangram-button--TANGRAM_52")));
|
|
Actions dragger = new Actions(driver);
|
|
dragger.moveToElement(draggablePartOfScrollbar).moveByOffset(0,draggablePartOfScrollbar.getSize().getHeight()-100).clickAndHold().moveByOffset(0,1000).release().perform();
|
|
Thread.sleep(1000);
|
|
}catch (Exception e){
|
|
System.out.println("try" + i);
|
|
}
|
|
}
|
|
WebElement input = (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.linkText("涓嬩竴椤�")));
|
|
input.click();
|
|
Thread.sleep(3000);
|
|
}
|
|
}
|
|
}
|