web自动化—selenium+java实现某站自动登录

        很多铁子都喜欢看b站,通过b站我们可以获取快乐,可以获取知识,还可以提高认知;其中更是有一些作者我们非常的喜欢,也想为这些作者点赞鼓励支持,很想给他们的作品三连,为他加油,让他们有更多好作品分享给我们!不然作者丧失了更新的动力,对我们来说也是一种损失。

        现在就是有一个问题,作者的作品那么多,我们一个一个手动三连肯定是很难受的,我们能不能借助一些知识来帮我们完成这个事情呢,欸,这里博主就帮大家想到了这个问题,并且也有相对应的手段,博主在这里给大家分享一个用web自动化Selenium模块的操作技巧,让它来帮我们对喜欢的作者的作品完成三连!效果非常好,话不多说,直接开始分享,这个是java版本的,其他语言思路是一样的,改一个方法就行了。

一、模块安装

        selenium的安装参考博主另外一个文章,JAVA配合selenium包对浏览器进行操作小魏快起床的博客-CSDN博客selenium配合java使用

安装好之后就可以起飞了

        

二、注意点

        先说注意点,因为b站有登录检测,这个功能还没做,所以需要用debug运行,将断点打在下面这个代码之前,卡住,然后登录输入账号和密码,然后点击登录,这个时候再放开代码运行后面的业务;

        //手动点击呜呜呜
        System.out.println("手动点击成功");

三、上代码

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: x
 * @Date: 2022/12/08/16:52
 * @Description: 给好朋友的b站三连
 */
public class BiZhanSanLian {

    //访问路径 不同的作者只需要更改url里面的数字就可以了 
//    public static String url = "https://space.bilibili.com/2060764481/video";
    public static String url = "https://space.bilibili.com/284485909/video";
    //账号 密码
    public static String username = "";
    public static String password = "";

    //点赞控制是否开启 true开启 false关闭
    public static Boolean dzState = true;

    //投币控制是否开启 true开启 false关闭【如果币不足会影响收藏的执行】
    public static Boolean tbState = false;

    //收藏控制是否开启 true开启 false关闭
    public static Boolean scState = true;


    public static void main(String[] args) throws InterruptedException {
        //1、获取谷歌浏览器控制 打开浏览器
        WebDriver driver = WebDriverUtils.getNormalWebDriver();

        //2、操作b站,实现业务
        realizeTriple(driver);

        //3、关闭浏览器
        driver.close();
    }

    public static void realizeTriple(WebDriver driver) throws InterruptedException {
        //1、打开浏览器
        driver.get(url);
        TimeUnit.SECONDS.sleep(2);
        //登录
        driver.findElement(By.className("unlogin-avatar")).click();
        driver.findElement(By.cssSelector("body > div.bili-mini-mask > div > div.bili-mini-content > div.bili-mini-login-wrapper > div.bili-mini-password-wrapper > div.bili-mini-account > input[type=text]")).sendKeys(username);
        driver.findElement(By.cssSelector("body > div.bili-mini-mask > div > div.bili-mini-content > div.bili-mini-login-wrapper > div.bili-mini-password-wrapper > div.bili-mini-password > div.left > input[type=password]")).sendKeys(password);

        //手动点击呜呜呜
        System.out.println("手动点击成功");

        //获取首页的句柄
        String indexHandle = driver.getWindowHandle();

        //先获取有多少个大页面[1个大页面30个视频]
        String pageText = driver.findElement(By.className("be-pager-total")).getText();
        pageText = pageText.replace("共", "")
                .replace("页", "")
                .replace(",", "")
                .replace(" ", "");
        //处理pageText,将字符串设置为数字
        int pageNum = Integer.parseInt(pageText);

        //循环对每一页的视频进行三连
        for (int i = 1; i <= pageNum; i++) {
            //如果是第一页,直接执行
            if (i == 1) {
                Container(driver, indexHandle);
                continue;
            }
            //如果是第二页开始,就先找到输入框,输入对应的页数并模拟键盘enter
            driver.findElement(By.xpath("//*[@id=\"submit-video-list\"]/ul[3]/span[2]/input")).sendKeys("" + i);
            driver.findElement(By.xpath("//*[@id=\"submit-video-list\"]/ul[3]/span[2]/input")).sendKeys(Keys.ENTER);

            System.out.println("成功跳转到" + i + "页");
            //开始逻辑运行
            Container(driver, indexHandle);
        }
        System.out.println("全部完成!!");

    }


    /*核心操作,三连*/
    public static void Container(WebDriver driver, String indexHandle) throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        //1、检测页面里面的视频ul  类名是cube-list
        WebElement cubeListUl = driver.findElement(By.className("cube-list"));
        //2、找ul下面的所有li
        List<WebElement> liList = cubeListUl.findElements(By.xpath("li"));
        //3、循环集合
        for (WebElement li : liList) {
            //3.1、点击进入到详情页
            li.click();

            //3.2、切换到新页面 需要切换句柄
            driver.switchTo().window(new ArrayList<>(driver.getWindowHandles()).get(1));

            //设置点延时 等待页面加载
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //获取一些关键信息
            System.err.println("log-info:进入到" + driver.getTitle() + "作品详情页面,正在打赏三连~~~");

            //3.3、找手指按钮然后点击
            if (dzState) {
                try {
                    WebElement element = driver.findElement(By.xpath("//*[@id=\"arc_toolbar_report\"]/div[1]/span[1]"));
                    //判断是否有on
                    String dzBtnClass = element.getAttribute("class");
                    if (!dzBtnClass.endsWith("on")) {
                        element.click();
                        System.out.println("点赞成功!");
                    }
                } catch (Exception e) {
                    System.out.println("点击失败了,GG!");
                    e.printStackTrace();
                }
            }

            if (tbState) {
                try {
                    //3.4、找到投币标签元素并投币 //会弹出投币框 找到确定然后点击
                    WebElement element = driver.findElement(By.className("coin"));
                    if (!element.getAttribute("class").endsWith("on")) {
                        element.click();
                        driver.findElement(By.className("bi-btn")).click();
                        //判断硬币是否足够
                        System.out.println("投币成功!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("投币已经投过了");
                }
            }

            if (scState) {
                //3.5、收藏
                try {
                    WebElement element = driver.findElement(By.className("collect"));
                    if (!element.getAttribute("class").endsWith("on")) {
                        element.click();
                        driver.findElement(By.xpath("//*[@id=\"app\"]/div[4]/div[3]/div/div/div[2]/div/ul/li/label/span[1]")).click();
                        driver.findElement(By.className("submit-move")).click();
                        System.out.println("收藏成功!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("该文章已经被收藏过!!");
                }

            }


            //设置点延时
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //3.6、关闭当前页面 且 切换为主页面的句柄【窗口】
            driver.close();
            driver.switchTo().window(indexHandle);
        }

    }


}

这里面需要获取驱动,博主将获取驱动的方法封装在另外的一个类,所以大家需要先把下面这个类放进去,也可以参考前面搭建驱动的博客进行一个学习,需要的配置文件路径请自行更换

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xjx
 * @Date: 2022/09/22/17:00
 * @Description: 关于selenium获取driver驱动的工具类
 */
public class WebDriverUtils {

    private final static String driver = "webdriver.chrome.driver";
    private final static String chromeDriver = "C:\\Users\\d\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe";
    private final static String openCvDll = "D:\\Program Files (x86)\\opencv\\build\\java\\x64\\opencv_java460.dll";

    static {
        // 引入谷歌驱动执行文件 控制浏览器
        System.setProperty(driver, chromeDriver);
        // 引入opencv的dll文件
        System.load(openCvDll);
    }

    /*
     * 1、获取开启无头模式的驱动
     * */
    public static WebDriver getHeadLessWebDriver() {
        //1、参数设置,无头模式
        ChromeOptions options = new ChromeOptions();
        options.addArguments("-headless");
        WebDriver driver = new ChromeDriver(options);
        //2、超时等待五秒
        Duration duration = Duration.ofSeconds(5);
        driver.manage().timeouts().implicitlyWait(duration);
        //3、机器标识符关闭
        ((JavascriptExecutor) driver).executeScript("Object.defineProperties(navigator,{ webdriver:{ get: () => false } })");
        return driver;
    }

    /*
     * 2、获取正常的谷歌驱动
     * */
    public static WebDriver getNormalWebDriver() {
        //1、参数设置,无头模式
        ChromeOptions options = new ChromeOptions();
        //2、隐藏 navigator.webdriver
        options.addArguments("--disable-blink-features=AutomationControlled");
        //3、关闭左上方Chrome 正受到自动测试软件的控制的提示
        options.setExperimentalOption("useAutomationExtension", false);
        WebDriver driver = new ChromeDriver(options);
        //2、超时等待五秒
        Duration duration = Duration.ofSeconds(5);
        driver.manage().timeouts().implicitlyWait(duration);
        //3、机器标识符关闭
        ((JavascriptExecutor) driver).executeScript("Object.defineProperties(navigator,{ webdriver:{ get: () => false } })");
        return driver;
    }

}

四、总结

博客旨在分享技术,互相学习,请勿乱来

觉得有帮助的铁子们,多多支持博主,我知道你们看过会点赞收藏的,博主先在这儿谢过!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值