java使用selenium3

selenium的介绍及背景这里不再说明,直接百度能看到很多文章,话不多说上代码

// 在System中配置谷歌驱动的路劲
System.setProperty("webdriver.chrome.driver","C:\\Users\\0\\Desktop\\2019-07-01\\chromedriver.exe");
// 设置启动参数
ChromeOptions options = new ChromeOptions();
//  设置浏览器window.navigator.webdriver 为false
options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));

// 打开浏览器
EventFiringWebDriver driver = new EventFiringWebDriver(new ChromeDriver(options));
driver.register(new MyWebDriverListener());

// 设置浏览器窗口分辨率大小
driver.manage().window().setSize(new Dimension(480, 1200));

String url = “https://www.baidu.com”;
// 加载链接
driver.get(url);
// 查找元素
WebElement emailHeatmap = driver.findElement(By.id("xxxx"));
// 执行js
((JavascriptExecutor) driver).executeScript("$(\"#confirm-booking-heatmap\").click()");


/**
实现WebDriverEventListener接口监听事件
**/
class MyWebDriverListener implements WebDriverEventListener{

	@Override
	public void beforeAlertAccept(WebDriver driver) {
		System.out.println("beforeAlertAccept:"+LocalDateTime.now());
	}

	@Override
	public void afterAlertAccept(WebDriver driver) {
		System.out.println("afterAlertAccept:"+LocalDateTime.now());
	}

	@Override
	public void afterAlertDismiss(WebDriver driver) {
		System.out.println("afterAlertDismiss:"+LocalDateTime.now());
	}

	@Override
	public void beforeAlertDismiss(WebDriver driver) {
		System.out.println("beforeAlertDismiss:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeNavigateTo(String url, WebDriver driver) {
		System.out.println("beforeNavigateTo:"+url+","+driver.getCurrentUrl());
		
	}

	@Override
	public void afterNavigateTo(String url, WebDriver driver) {
		System.out.println("afterNavigateTo:"+url+","+driver.manage());
		
	}

	@Override
	public void beforeNavigateBack(WebDriver driver) {
		System.out.println("beforeNavigateBack:"+LocalDateTime.now());
		
	}

	@Override
	public void afterNavigateBack(WebDriver driver) {
		System.out.println("afterNavigateBack:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeNavigateForward(WebDriver driver) {
		System.out.println("beforeNavigateForward:"+LocalDateTime.now());
		
	}

	@Override
	public void afterNavigateForward(WebDriver driver) {
		System.out.println("afterNavigateForward:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeNavigateRefresh(WebDriver driver) {
		System.out.println("beforeNavigateRefresh:"+LocalDateTime.now());
		
	}

	@Override
	public void afterNavigateRefresh(WebDriver driver) {
		System.out.println("afterNavigateRefresh:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeFindBy(By by, WebElement element, WebDriver driver) {
		System.out.println("beforeFindBy:"+LocalDateTime.now());
		
	}

	@Override
	public void afterFindBy(By by, WebElement element, WebDriver driver) {
		System.out.println("afterFindBy:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeClickOn(WebElement element, WebDriver driver) {
		System.out.println("beforeClickOn:"+LocalDateTime.now());
		
	}

	@Override
	public void afterClickOn(WebElement element, WebDriver driver) {
		System.out.println("afterClickOn:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		System.out.println("beforeChangeValueOf:"+LocalDateTime.now());
		
	}

	@Override
	public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
		System.out.println("afterChangeValueOf:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeScript(String script, WebDriver driver) {
		System.out.println("beforeScript:"+LocalDateTime.now());
		
	}

	@Override
	public void afterScript(String script, WebDriver driver) {
		System.out.println("afterScript:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeSwitchToWindow(String windowName, WebDriver driver) {
		System.out.println("beforeSwitchToWindow:"+LocalDateTime.now());
		
	}

	@Override
	public void afterSwitchToWindow(String windowName, WebDriver driver) {
		System.out.println("afterSwitchToWindow:"+LocalDateTime.now());
		
	}

	@Override
	public void onException(Throwable throwable, WebDriver driver) {
		System.out.println("onException记录:"+LocalDateTime.now());
		
	}

	@Override
	public  void beforeGetScreenshotAs(OutputType target) {
		System.out.println("beforeGetScreenshotAs:"+LocalDateTime.now());
		
	}

	@Override
	public  void afterGetScreenshotAs(OutputType target, X screenshot) {
		System.out.println("afterGetScreenshotAs:"+LocalDateTime.now());
		
	}

	@Override
	public void beforeGetText(WebElement element, WebDriver driver) {
		System.out.println("WebElement:"+LocalDateTime.now());
		
	}

	@Override
	public void afterGetText(WebElement element, WebDriver driver, String text) {
		System.out.println("afterGetText:"+LocalDateTime.now());
		
	}
	
}

// 火狐启动方式
System.setProperty("webdriver.gecko.driver","C:\\Users\\0\\Desktop\\2019-07-01\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

谷歌浏览器的webdriver的下载地址
http://npm.taobao.org/mirrors/chromedriver

maven引入


	    org.seleniumhq.selenium
	    selenium-java
	    3.141.59
	

打开多个标签页和在多个标签中切换

 // 利用js代码打开标签页
 ((JavascriptExecutor) driver).executeScript("window.open(\"" + keyUrl + "\");");
 // 将页签且回到第一个
 Set windows = driver.getWindowHandles();
 List list = new ArrayList<>(windows);
 driver.switchTo().window(list.get(0));

你可能感兴趣的:(爬虫,爬虫)