0
点赞
收藏
分享

微信扫一扫

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境

 使用 java 搭建 selenium 环境,进行开发,步骤如下

1、下载浏览器对应版本驱动

笔者这里以谷歌浏览器为例

先确定谷歌浏览器版本,打开浏览器,点击帮助、关于Google Chrome

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_maven

查看浏览器版本

 Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_chrome_02

下载谷歌浏览器对应版本驱动

下载地址:​​http://chromedriver.storage.googleapis.com/index.html​​

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_微服务_03

查找对应版本,这里可能出现没有正对的版本,如果没有正对的版本,下载一个接近的版本

笔者当前浏览器版本没有正对的,下载了一个相近的版本

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_java_04

下载后解压出来

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_微服务_05

 笔者这里将其复制到另一个文件夹中,以方便后续使用

Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis+ElementUI之Java 搭建 Selenium 环境_maven_06

2、编写代码

新建一个maven项目

添加 selenium-java 的依赖

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>

 笔者的 pom文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wsjzzcbq</groupId>
<artifactId>selenium-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

 新建 Demo示例

package com.wsjzzcbq.selenium.demo;

import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo {

public static void main(String[] args) {
//驱动地址
String chromedriver = "D:\\tmp\\chromedriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromedriver);
//初始化一个chrome浏览器实例
WebDriver webDriver = new ChromeDriver();
//最大化窗口
webDriver.manage().window().maximize();
//设置隐性等待时间
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
//打开百度
webDriver.get("https://www.baidu.com");
}

}

chromedriver 的地址是 chromedriver.exe 的地址

上面代码运行后,会启动谷歌浏览器并自动打开百度

 


举报

相关推荐

0 条评论