00菜鸡猿00 2023-02-25 17:34 采纳率: 0%
浏览 334
已结题

使用 selenium 自动操作浏览器,如何避开 CloudFlare 5秒盾的检测

1、运行环境:Java + selenium + chrome浏览器108版本 + chromedriver

注:如果Java不能避开检测,也可以使用Python,只求正确方法

2、问题

公司业务:需要自动操作浏览器进行“登陆账号”和“商品库存数量的增加”
问题发生背景:selenium 自动操作浏览器访问 https://www.kickstarter.com/,会被 CloudFlare 检测到,特别在点击登陆之后
错误图片:

img

解答思路和尝试过的方法:
  • 修改谷歌浏览器启动参数
  • 更改访问ip
附代码

public static void main(String[] args) {
    WebDriver webDriver = null;
    try {
        // 设置 chromedirver 的存放位置
        System.getProperties().setProperty("webdriver.chrome.driver", CHROME_DRIVER);
        ChromeOptions chromeOptions = new ChromeOptions();
        //chrome添加参数
        addArguments(chromeOptions);
        // 实例化
        webDriver = new ChromeDriver(chromeOptions);
        // 1.模拟打开登陆页面
        webDriver.get(WEB_URL);

        /**
         * TODO:CloudFlare拦截有几种触发方式
         * 1、打开页面便会被检测 CloudFlare 到并拦截
         * 2、点击登陆或点击其他按钮跳转到其他页面都会被拦截
         */
        
        // 显式等待
        // 2.等10秒钟响应后再操作,不然内容可能还没有返回
        WebDriverWait wait = new WebDriverWait(webDriver, 10);
        // 查找id为“kw"的元素是否加载出来了(已经在页面DOM中存在)
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("user_session_email"))); // 在设定时间内找到后就返回,超时直接抛异常

        // ......

    } catch (Exception e) {
        e.printStackTrace();
        assert webDriver != null;
        File srcFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        //图片名称加时间戳
        String dateString = getDateFormat();
        // 需要指定图片的保存路径及文件名
        try {
            FileUtils.copyFile(srcFile, new File("D:\\selenium\\" + dateString + ".png"));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    } finally {
        if (webDriver != null) {
            webDriver.quit();
        }
    }
}

/**
 * chrome添加参数
 */
public static void addArguments(ChromeOptions options){
    // 不加载图片, 提升速度
    // 关闭界面上的---Chrome正在受到自动软件的控制
    options.addArguments("disable-infobars");
    // 浏览器不提供可视化页面. linux下不支持可视化不加这条会启动失败
    //options.addArguments("--headless");
    // 启动无沙盒模式运行,以最高权限运行
    options.addArguments("--no-sandbox");
    // 优化参数
    // 不加载图片, 提升速度
    options.addArguments("blink-settings=imagesEnabled=false");
    // 禁用gpu渲染
    options.addArguments("--disable-gpu");
    options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
    //设置为开发者模式
    options.addArguments("--disable-blink-features=AutomationControlled");
    options.addArguments("--user-agent=Mozilla/5.0 HAHA");
    //  隐藏滚动条, 应对一些特殊页面
    options.addArguments("--hide-scrollbars");
    // 优化参数
    // 指定用户文件夹User Data路径,可以把书签这样的用户数据保存在系统分区以外的分区
    options.addArguments("--user-data-dir=D:\\selenium\\ChromeProfile");
    //chrome.exe --remote-debugging-port=9333 --user-data-dir="D:\selenium\ChromeProfile"
    //options.setExperimentalOption("debuggerAddress", "127.0.0.1:9333");

    //设置访问的ip
    options.addArguments("--proxy-server=http://36.26.106.179:4223");

}
  • 写回答

8条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-02-25 20:41
    关注
    评论

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 创建了问题 2月25日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表