Selenium 远程调用 Google Chrome 谷歌浏览器

Selenium 远程调用 Google Chrome 谷歌浏览器

我最近又使用谷歌浏览远程调用发现不能使用了

参考连接
具体原因是因为
谷歌浏览器在11几的版本(目前是:114.0.5735.91)之后只能使用JDK高版本我目前使用的是JDK17版本远程调用如果是低版本比如102左右的是好使的,新使用方法我更新在下面

一. 前沿

每次重新运行Selenium都直接弹出来,运行的次数多了菜单栏一堆谷歌浏览器
,远程就可以解决重复弹出框的问题,还可以解决钉钉浏览器无法登录等问题
.有爬虫检测的页面都可以用这个解决

二. 方法

谷歌新版本增加:
使用JDK17
selenium-java 4.9.0
增加 selenium-http-jdk-client pom
以下是完整pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>selenium17</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>selenium17</name>
    <description>selenium17</description>
    <properties>
        <java.version>17</java.version>
        <selenium.version>4.9.0</selenium.version>
        <jsoup.version>1.14.3</jsoup.version>
        <pagehelper.version>1.2.5</pagehelper.version>
        <druid.version>1.1.23</druid.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- pagehelper 分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>



        <!-- selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-http-jdk-client</artifactId>
            <version>4.5.0</version>
        </dependency>
        <!-- jsoup-java -->
        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>${jsoup.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.首先找到桌面的谷歌浏览器
在这里插入图片描述
2.鼠标右键打开属性
在这里插入图片描述
3. 在目标里更改 ,改完之后直接双击点开谷歌浏览器就可以了

1.原先是这样的
C:\Users\13686\AppData\Local\Google\Chrome\Application\chrome.exe
2.在这个后面加上 --user-data-dir 路径自己定义随意,他会自己创建的
–remote-debugging-port=9222 --user-data-dir=“D:\WORK\selenium\ChromeProfile”
3.完整语句
C:\Users\13686\AppData\Local\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222 --user-data-dir=“D:\WORK\selenium\ChromeProfile”

4.然后代码上连接

package com.selenium.demo;

import com.selenium.demo.Util.JsoupUtil;
import com.selenium.demo.Util.SeleniumUtil;
import com.selenium.demo.Util.UModalDialogsUtil;
import com.selenium.demo.Util.UUIDUtil;
import com.selenium.demo.domain.Dom;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Collections;
import java.util.List;

public class SeleniumDemo {

    //访问地址
    private static final String url = "http://localhost/system/dict";

    public static void main(String[] args) throws InterruptedException {
		
        //设置驱动,后面的路径自己要选择正确,也可以放在本地
        System.setProperty("webdriver.chrome.driver", "D:\\WORK\\selenium\\chromedriver.exe");
        //谷歌新版本增加
        System.setProperty("webdriver.http.factory", "jdk-http-client");
		//创建设置
        ChromeOptions options = new ChromeOptions();
		//然后前面debuggerAddress 这个不用动 只改后面的地址就是浏览器设置的地址
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
		//谷歌新版本增加
		options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);

        driver.get(url);

    }
}

这时候WebDriver 不会给你打开浏览器了,需要你自己打开浏览器,然后运行代码这时候页面就自己加载了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我有一个抱枕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值