很久没有写文章分(装)享(逼)了,趁着国庆节有充足的时间分享一下最近所学。 html
最近被分到一个活,给你一个视频地址,须要播放这个视频并录屏保存java
由于要模拟打开网页的操做,须要使用浏览器,因此想使用无头浏览器去实现。无头浏览器有不少种,由于本身学习的语言是java,因此重点调查了支持java语言的无头浏览器。比较经常使用的是PhantomJS、Selenium、jBrowserDriver。最终肯定使用Selenium,由于另外两种方式不支持flash播放,那么有些视频网站的地址就不能正确播放。 对于登录的问题,原本决定使用cookie登录,后来在写Selenium简单使用例子的时候以为能够经过获取网页的XPath,模拟填入内容和点击的效果。git
由于selenium须要依赖驱动,因此首先得安装drive。以google浏览器为例,须要安装chromedriver。须要注意:github
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at com.shuwen.test.Selenium2Test.main(Selenium2Test.java:15)
复制代码
//指定chromedriver安装位置,注意:必须和chrome在同个目录下
System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/chromedriver");
//设置google浏览器选项
ChromeOptions chromeOptions = new ChromeOptions();
//指定浏览器全屏
chromeOptions.addArguments("--start-fullscreen");
//建立chrome
WebDriver driver = new ChromeDriver(chromeOptions);
复制代码
经过 ChromeOptions设置了一些浏览器参数,好比但愿浏览器全屏打开 执行此段代码之后会发现弹出了一个浏览器,而且打开了指定的页面,可是: web
//*[@id="ykPlayer"]/div[2]/div[2]/div[5]/div[2]/a
模拟点击driver.findElement(By.xpath("//*[@id=\"ykPlayer\"]/div[2]/div[2]/div[5]/div[2]/a")).click();
复制代码
.click 模拟点击操做chrome
//*[@id=\"YT-ytaccount\"]
密码xpath://*[@id=\"YT-ytpassword\"]
登录xpath://*[@id=\"YT-nloginSubmit\"]
driver.findElement(By.xpath("//*[@id=\"YT-ytaccount\"]")).sendKeys("YourAccount");
driver.findElement(By.xpath("//*[@id=\"YT-ytpassword\"]")).sendKeys("YourPassword");
driver.findElement(By.xpath("//*[@id=\"YT-nloginSubmit\"]")).click();
复制代码
.sendKey往输入框填入指定数据api
driver.quit();
复制代码
package com.cqupt.login.youku;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
/**
* 模拟登录优酷
*
* @author hetiantian
* @date 2018/10/01
* */
public class YouKuLogin {
public static void main(String[] args) throws InterruptedException {
//指定chromedriver安装位置,注意:必须和chrome在同个目录下
System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/chromedriver");
//设置google浏览器选项
ChromeOptions chromeOptions = new ChromeOptions();
//指定浏览器全屏
chromeOptions.addArguments("--start-fullscreen");
//建立chrome
WebDriver driver = new ChromeDriver(chromeOptions);
// 使用它访问 Google
driver.get("https://v.youku.com/v_show/id_XMzYxMDM4MzY3Ng==.html");
Thread.sleep(2*1000);
//模拟点击登录
driver.findElement(By.xpath("//*[@id=\"ykPlayer\"]/div[2]/div[2]/div[5]/div[2]/a")).click();
Thread.sleep(2*1000);
//模拟点击登录
driver.findElement(By.xpath("//*[@id=\"YT-ytaccount\"]")).sendKeys("YourAccount");
driver.findElement(By.xpath("//*[@id=\"YT-ytpassword\"]")).sendKeys("YourPassword");
driver.findElement(By.xpath("//*[@id=\"YT-nloginSubmit\"]")).click();
//让视频加载完成
Thread.sleep(2000);
// 检查页面标题
System.out.println("Page title is: " + driver.getTitle());
//退出浏览器
driver.quit();
}
}
复制代码
附:gitub地址:github.com/TiantianUpu…浏览器