栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA+Selenium+Chrome+Chromedriver 模拟浏览器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA+Selenium+Chrome+Chromedriver 模拟浏览器

linux 的环境准备

安装 google-chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
下载 Chromedriver
地址:http://npm.taobao.org/mirrors/chromedriver/ (根据google-chrome -version找对应版本)
下载后放到根目录,注意要给 chromedriver 可执行权限:chmod 777 /chromedriver

windows 的环境准备

安装 google-chrome 浏览器
下载 Chromedriver
地址:http://npm.taobao.org/mirrors/chromedriver/ (根据google-chrome -version找对应版本)

JAVA Maven

maven 导入包

    org.seleniumhq.selenium
    selenium-java
    2.33.0

工具类例子

package c.o.v;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class YXMUtil {

    public static void main(String[] args) {
        get("https://mall.q-gp.com/share/product?skuNo=366936136878081");
    }

    public static Map get(String url) {
        // 参数配置
        System.setProperty("webdriver.chrome.driver", "/chromedriver.exe");
        WebDriver driver;
        ChromeOptions option = new ChromeOptions();
        option.addArguments("headless"); // 无界面参数
        option.addArguments("no-sandbox"); // 禁用沙盒
        // 通过ChromeOptions的setExperimentalOption方法,传下面两个参数来禁止掉谷歌受自动化控制的信息栏
        option.setExperimentalOptions("useAutomationExtension", false);
        option.setExperimentalOptions("excludeSwitches", Collections.singletonList("enable-automation"));
        driver = new ChromeDriver(option);
        driver.get(url);
        String title = "";
        String num = "";
        String img = "";
        long time = System.currentTimeMillis();
        while (StringUtils.isEmpty(img) || StringUtils.isEmpty(title) || StringUtils.isEmpty(num)) {
            img = driver.findElement(By.className("cr-image--img")).getAttribute("src");
            title = driver.findElement(By.className("title")).getText();
            num = driver.findElement(By.className("num")).getText();
            if (System.currentTimeMillis() - time > 5000) {
                System.out.println("获取超时,已结束");
                driver.quit();
                return null;
            }
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("标题:" + title);
        System.out.println("价格:" + num);
        System.out.println("图片:" + img);
        driver.quit();
        Map map = new HashMap();
        map.put("title", title);
        map.put("num", num);
        map.put("img", img);
        return map;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825372.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号