java模拟登录优酷

很久没有写文章分(装)享(逼)了,趁着国庆节有充足的时间分享一下最近所学。 html

我爱学习.jpg

需求背景

最近被分到一个活,给你一个视频地址,须要播放这个视频并录屏保存java

步骤

  • 打开网页
  • 登录
  • 播放
  • 录屏

疑难问题

  • 有些视频须要登录之后才能播放
  • 有些网站播放须要安装flash

前期调研

由于要模拟打开网页的操做,须要使用浏览器,因此想使用无头浏览器去实现。无头浏览器有不少种,由于本身学习的语言是java,因此重点调查了支持java语言的无头浏览器。比较经常使用的是PhantomJS、Selenium、jBrowserDriver。最终肯定使用Selenium,由于另外两种方式不支持flash播放,那么有些视频网站的地址就不能正确播放。 对于登录的问题,原本决定使用cookie登录,后来在写Selenium简单使用例子的时候以为能够经过获取网页的XPath,模拟填入内容和点击的效果。git

前期准备

由于selenium须要依赖驱动,因此首先得安装drive。以google浏览器为例,须要安装chromedriver。须要注意:github

  • 安装的chromedriver须要和google版本一致
  • 安装位置必须和google同一路径,不然将会报错:
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)
复制代码
  • 添加selenium依赖

编码实现

  • 打开浏览器
//指定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

页面.png
须要点击登录一下才能登录

  • 模拟点击登录 检查该页面
    点击.png
    能够很快的得到xpath为//*[@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

  • 输入帐号密码登录
    image.png
    以一样的方式得到对应的xpath路径 帐号xpath://*[@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();
复制代码
  • 完整demo
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…浏览器

相关文章
相关标签/搜索