启用远程调用Docker下应用selenium-chrome服务
星期五, 2020-09-18 | Author: Lee | JAVA-and-J2EE, linux | 1,896 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玩耍了,常用的功能都已经支持.
文章作者: Lee
本文地址: https://www.pomelolee.com/2100.html
除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址
No comments yet.
Leave a comment
Search
相关文章
热门文章
最新文章
文章分类
- ajax (10)
- algorithm-learn (3)
- Android (6)
- as (3)
- computer (85)
- Database (30)
- disucz (4)
- enterprise (1)
- erlang (2)
- flash (5)
- golang (3)
- html5 (18)
- ios (4)
- JAVA-and-J2EE (186)
- linux (143)
- mac (10)
- movie-music (11)
- pagemaker (36)
- php (50)
- spring-boot (2)
- Synology群晖 (2)
- Uncategorized (6)
- unity (1)
- webgame (15)
- wordpress (33)
- work-other (2)
- 低代码 (1)
- 体味生活 (40)
- 前端 (21)
- 大数据 (8)
- 游戏开发 (9)
- 爱上海 (19)
- 读书 (4)
- 软件 (3)