1、安装docker

        省略

2、拉取镜像

docker pull selenium/standalone-chrome-debug

3、运行容器

docker run -d -p 4444:4444 -p 5900:5900 -v C:\Users\Public\VNC_Donwnloads:/home/seluser/Downloads --memory=6g --name selenium_chrome selenium/standalone-chrome-debug

其中 4444 是连接端口,5900是 vnc远程连接接口,内存限制6g 建议设置成2g(博主主机内存大),

C:\Users\Public\VNC_Donwnloads:/home/seluser/Downloads 其中C:\Users\Public\VNC_Donwnloads 是下载主机目录,因为博主docker在wsl内。

/home/seluser/Downloads 是容器内chrome 下载文件的地址(固定的,不用去改动)

4、连接容器桌面

连接vnc可以查看到容器内桌面,方便开发调试

下载vnc远程连接工具 推荐下载:RealVNC® - Remote access software for desktop and mobile | RealVNC

输入  ip:端口  和密码, 密码 默认为secret

成功进入 fluxbox桌面

可以看到已经安装好了chrome

5、编写测试代码 

博主使用java进行编写 测试代码,当然使用其他语言也是类似的

maven

org.seleniumhq.selenium

selenium-java

4.17.0

示例:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;

import java.net.URL;

public class Main {

public static void main(String[] args) {

WebDriver driver=null;

try {

// 远程Selenium 服务器地址

URL seleniumHub=new URL("http://localhost:4444/wd/hub");

// 创建options

ChromeOptions options=new ChromeOptions();

// 创建远程WebDriver

driver= new RemoteWebDriver(seleniumHub,options);

// 跳转百度界面

driver.get("https://www.baidu.com");

// 获取页面html

String html = driver.getPageSource();

System.out.println(html);

// 5秒后结束程序

Thread.sleep(5000);

}catch (Exception e){

e.printStackTrace();

}finally {

if(driver!=null){

// 关闭窗口

driver.close();

// 关闭程序

driver.quit();

}

}

}

}

效果:

觉得对你有帮助欢迎   点赞  收藏。 

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: