java模拟登陆优酷

好久没有写文章分(装)享(逼)了,趁着国庆节有充足的时间分享一下最近所学。
我爱学习.jpg

需求背景

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

步骤

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

疑难问题

  • 有些视频需要登陆以后才能播放
  • 有些网站播放需要安装flash

前期调研

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

前期准备

因为selenium需要依赖驱动,所以首先得安装drive。以google浏览器为例,需要安装chromedriver。需要注意:

  • 安装的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.htmlat 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.(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设置了一些浏览器参数,比如希望浏览器全屏打开
执行此段代码以后会发现弹出了一个浏览器,并且打开了指定的页面,但是:
页面.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 模拟点击操作

  • 输入账号密码登陆
    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往输入框填入指定数据

  • 退出浏览器
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");//创建chromeWebDriver driver = new ChromeDriver(chromeOptions);// 使用它访问 Googledriver.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地址:https://github.com/TiantianUpup/login-demo


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部