启用远程调用Docker下应用selenium-chrome服务

星期五, 2020-09-18 | Author: Lee | JAVA-and-J2EE, linux | 237 views

1.服务调用本地的chromedriver驱动部署到服务器上比较麻烦,借助docker可以快速使用和独立

docker-selenium 的git地址:https://github.com/SeleniumHQ/docker-selenium

如果未安装docker可以一键安装

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
#或者:国内源速度快点
curl -sSL https://get.daocloud.io/docker | sh
##启动docker
sudo systemctl start docker
 
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:4.0.0-alpha-7-prerelease-20200907

2.启用chrome的支持cdp功能远程驱动

 
private static final String CHROME_REMOTE_URL = "http://localhost:4444/wd/hub";
driver = new CdpRemoteWebDriver(new URL(CHROME_REMOTE_URL), chromeOptions);
 
public class CdpRemoteWebDriver extends RemoteWebDriver {
 
    private static final Map<String, CommandInfo> CHROME_COMMAND_NAME_TO_URL = new HashMap<>();
 
    public CdpRemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
 
        super((CommandExecutor)(new HttpCommandExecutor(ImmutableMap.copyOf(CHROME_COMMAND_NAME_TO_URL), remoteAddress)), capabilities);
 
    }
 
    public Map<String, Object> executeCdpCommand(String commandName, Map<String, Object> parameters) {
 
        Objects.requireNonNull(commandName, "Command name must be set.");
 
        Objects.requireNonNull(parameters, "Parameters for command must be set.");
 
        @SuppressWarnings("unchecked")
		Map<String, Object> toReturn = (Map<String, Object>) this.getExecuteMethod().execute("executeCdpCommand", ImmutableMap.of("cmd", commandName, "params", parameters));
 
        return ImmutableMap.copyOf(toReturn);
 
    }
 
    static {
 
        CHROME_COMMAND_NAME_TO_URL.put("launchApp", new CommandInfo("/session/:sessionId/chromium/launch_app", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("getNetworkConditions", new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.GET));
 
        CHROME_COMMAND_NAME_TO_URL.put("setNetworkConditions", new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("deleteNetworkConditions", new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.DELETE));
 
        CHROME_COMMAND_NAME_TO_URL.put("executeCdpCommand", new CommandInfo("/session/:sessionId/goog/cdp/execute", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("getCastSinks", new CommandInfo("/session/:sessionId/goog/cast/get_sinks", HttpMethod.GET));
 
        CHROME_COMMAND_NAME_TO_URL.put("selectCastSink", new CommandInfo("/session/:sessionId/goog/cast/set_sink_to_use", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("startCastTabMirroring", new CommandInfo("/session/:sessionId/goog/cast/start_tab_mirroring", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("getCastIssueMessage", new CommandInfo("/session/:sessionId/goog/cast/get_issue_message", HttpMethod.GET));
 
        CHROME_COMMAND_NAME_TO_URL.put("stopCasting", new CommandInfo("/session/:sessionId/goog/cast/stop_casting", HttpMethod.POST));
 
        CHROME_COMMAND_NAME_TO_URL.put("setPermission", new CommandInfo("/session/:sessionId/permissions", HttpMethod.POST));
    }
}

3.可以和正常操作本地的chromedriver玩耍了,常用的功能都已经支持.

Tags: ,

文章作者: Lee

本文地址: https://www.pomelolee.com/2100.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Meta