前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java使用Selenium完成boss直聘自动打招呼脚本

java使用Selenium完成boss直聘自动打招呼脚本

作者头像
橘子君丶
发布2024-04-10 09:33:10
1490
发布2024-04-10 09:33:10
举报
文章被收录于专栏:springBoot3.0springBoot3.0

一、环境搭建

环境搭建参考博客

二、代码实现

1.导入maven依赖

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

2.java代码

代码语言:javascript
复制
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import java.util.List;

public class Main {
    static String msg = "您好,我有一坤年的工作经验,感觉和贵公司的岗位要求比较匹配,希望能进一步沟通下\n" +
                        "本人目前是离职状态,一周内可以到岗\n" +
                        "详情请查看我的简历,期望你的回复,谢谢您!";

    static Integer num = 0;

    public static void main(String[] args) throws Exception {
        // 谷歌驱动
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);

        // 打开登录页面
        webDriver.get("https://www.zhipin.com/web/user");

        // 用户需要在这两分钟内完成登录打开岗位查询页面
        int time = 0;
        int timeOut = 1000 * 60 * 2;
        while (time < timeOut) {
            Thread.sleep(1000);
            time += 1000;
            System.out.println("程序将在" + (timeOut - time) / 1000 + "秒后开启自动沟通,请打开岗位页面");
        }

        System.out.println("自动沟通开始...");
        // 遍历10页 偷懒写死了
        for (int page = 0; page < 10; page++) {
            // 获取这一页的工作岗位数量
            int jobNum = webDriver.findElements(By.className("job-card-wrapper")).size();
            for (int i = 0; i < jobNum; i++) {
                try {
                    hi(webDriver, i);
                } catch (Exception e) {
                    System.out.println("报错了:" + e.getMessage());
                    webDriver.navigate().refresh();
                    Thread.sleep(5000);
                }
            }
            try {
                // 下一页
                webDriver.findElement(By.className("ui-icon-arrow-right")).click();
            } catch (Exception e) {
                // 刷新页面重新尝试一次
                webDriver.navigate().refresh();
                Thread.sleep(5000);
                // 下一页
                webDriver.findElement(By.className("ui-icon-arrow-right")).click();
            }

            Thread.sleep(5000);
        }
    }

    public static void hi(WebDriver webDriver, int index) throws InterruptedException {
        // 回退页面后元素会刷新,需要重新获取一遍
        List<WebElement> jobList = webDriver.findElements(By.className("job-card-wrapper"));
        for (int i = 0; i < jobList.size(); i++) {
            if (i == index) {
                try {
                    // 使用Actions类进行悬浮
                    Actions actions = new Actions(webDriver);
                    actions.moveToElement(jobList.get(i)).perform();

                    WebElement btn = jobList.get(i).findElement(By.className("start-chat-btn"));
                    if (btn.getText().equals("继续沟通")) {
                        break;
                    }
                    // 点击沟通按钮
                    btn.click();
                    num++;
                } catch (Exception e) {
                    break;
                }

                Thread.sleep(1000);
                // 打招呼的消息
                webDriver.findElement(By.className("chat-input")).sendKeys(msg);
                // 发送
                webDriver.findElement(By.className("chat-input")).sendKeys(Keys.ENTER);
                System.out.println("沟通次数:" + num);
                Thread.sleep(1000);
                // 回退
                webDriver.navigate().back();
                Thread.sleep(2000);
                break;
            }
        }
    }
}

实现效果

1.启动main方法后会进入到登录页面,需要我们自己登录

2.登录后需要自己跳转到岗位搜索页面选择好搜索条件,等待2分钟后开启自动沟通

3.前面两个步骤需要自己操作,程序只是帮我们完成了打招呼的动作。2分钟倒计时结束后程序就会帮我们开启自动沟通了

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-09T,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、环境搭建
  • 二、代码实现
  • 实现效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档