Java+Selenium+Google浏览器实现Web自动化

一、背景

  本人非计算机专业,所以报了个成人高考,换个计算机专业,虽然没啥用。但是钱都花了所有不能敷衍了事。又因为某学校某网址的在线课程必须播放才能获得加分,所以利用自己已有知识和技能做了个小Java程序。
  程序功能很简单,就是自动化播放Web视频,视频结束了播放下一个。俗称唰客(懂得都懂好吧)。
在这里插入图片描述

二、环境准备

  • JDK1.8:Java程序必要环境;
  • Google浏览器:这里是用到的谷歌浏览器承载这个操作流程。不是用java请求响应实现的。
  • ChromeDriver:与谷歌浏览器对应的chromedriver.exe,用语代码与浏览器,代码与网页内容的交互。
  • Selenium:这里用到的java依赖,不好下载有些需要小梯子。
  • IDE:这里用到idea社区版 2022.1
  • 技能要求:需要对HTML,JS,JAVA有一定的掌握(通俗点就是能看懂,能读懂)。

三、解析

  因为地址和账号比较特殊,这里就截图只做解析。

(1) 登录界面解析,并完成登录

1.1 首先打开任意浏览器,在地址栏输入你想要做自动化的网址。
  • 举例:举例的地址se.mhtall.com/cuggw/rs/index
  • 功能分析:
    ①需要代码自动打开浏览器,并在地址栏输入登录地址,打开对应页面。
    ②打开登录地址后,找到对应的学号,密码输入框并输入值,实现登录功能
    登录界面
  • java源码编写

public static void main(String[] args) throws InterruptedException {
	   String usr = null;//设置变用户名
       String pwd = null;//设置变量密码
       String chromePath = null;//启动的浏览器的启动文件这里用的Google浏览器所以是“chrome.exe”
       String webdriverPath = null ;//web自动化驱动器“chromedriver.exe”,需要与浏览器版本对应
       if(args==null || (args !=null&&args.length<2) ){//这里是启动jar包传入参数,传参要求  参数1:用户 参数2:密码
           LogUtils.error("未设置登录参数!");
           throw new RuntimeException("未设置登录参数!");
       }
       if(args[0]!=null&& args[1]!=null){
           usr = args[0];
           pwd = args[1];
       }
       String  classLoader = System.getProperty("user.dir")+File.separator;
       if(classLoader==null){
           LogUtils.error("未资源路径classPath!");
           throw new RuntimeException("未资源路径classPath!");
       }
       if(args.length==3){//IDE测试执行时使用,后期无意义
           classLoader = "D:\\Work\\AI\\java\\runJava\\";
        }
       String relativelyPath = classLoader;
       //LogUtils.info("未资源路径relativelyPath:"+relativelyPath);
       //String relativelyPath = System.getProperty("user.dir") + File.separator + "lib" + File.separator;
       File[] fs = new File(relativelyPath).listFiles();
       for (File f : fs) {
           if (f.isDirectory()) {
               File[] files = f.listFiles();
               for(File fi:files) {
                   if(fi.isFile()&&fi.getName().indexOf("chrome.exe") !=-1) {
                       chromePath = fi.getPath();
                   }
               }
           }
           if (f.isFile() && f.getName().indexOf("chromedriver.exe") !=-1) {
               webdriverPath  = f.getPath();
           }
       }
       if(chromePath==null || webdriverPath ==null ){
           LogUtils.error("工具路径未设置!");
           throw new RuntimeException("工具路径未设置!");
       }

       WebDriver driver = null;
       ChromeOptions options = new ChromeOptions();
       //options.addEncodedExtensions(extension_path);
       options.setBinary(chromePath);
       // options.addArguments("--disable-extensions");
       options.addArguments("--headless");
       try {// 设置浏览器驱动程序路径
           System.setProperty("webdriver.chrome.driver", webdriverPath);
           // 创建Chrome浏览器驱动对象
           driver = new ChromeDriver();
           // 导航到网页,下面代码会自动的打开要get的地址
           driver.get("https://se.mhtall.com/cuggw/rs/index");
           }catch (Exception e){
           LogUtils.error("刷课数据异常:"+e.getMessage());
           e.printStackTrace();
       }finally {//关闭浏览器
           colos(driver);
       }
}

/**
    * 关闭浏览器
    * @param driver  自动化驱动对象
    */
   public static void colos(WebDriver driver){
       LogUtils.info("关闭浏览器。。。。。。。。。。");
       if (driver != null ){
           driver.quit();
       }
   }
1.2 开发者模式
  • 页面分析
    打开网页后点击快捷键F12,打开开发者模式查看源码,静态文件,请求,响应,调试,控制台。
    然后选择 元素 按钮,就可以看到HTML页面代码。
    打开开发者模式1
1
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A-Kar98k

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值