menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Selenium远程调试Chrome

作者:Chancel Yang, 创建:2022-10-26, 字数:1094, 已阅:33, 最后更新:2022-10-26

这篇文章更新于 540 天前,文中部分信息可能失效,请自行甄别无效内容。

因为使用远程开发,远程的机器是一台没有界面的Ubuntu1804,安装跟启动Chrome都是使用的headless模式,虽然方便但是看不到界面导致调试很麻烦

查了下资料发现Chrome支持远程调试,其开启方式如下

Batchfile
google-chrome-stable -remote-debugging-port=9222

但因为我的Ubuntu1804不方便安装桌面环境,所以考虑在另外一台Windows的主机上面启动Chrome来进行界面调试

Chrome的调试参数-remote-debugging-port没有找到监听外部IP的方法,默认开启的化只能监听本机127.0.0.1的端口

使用gost转发端口可以很好地解决这个问题

我的最终方案如下

Windows10主机(192.168.1.10)

安装Chrome,然后以远程调试模式运行

PowerShell
PS C:\Users\chancel> cd "C:\Program Files\Google\Chrome\Application"
PS C:\Program Files\Google\Chrome\Application> .\chrome.exe -remote-debugging-port=9222

下载gost,用于转发19222端口到9222端口

PowerShell
PS C:\Apps\bin> gost-windows-amd64.exe -L=tcp://:19222/127.0.0.1:9222

Ubuntu1804(192.168.1.20)

启动项目,安装selenium后,下载chromedriver,要以开发系统为准,比如这里我要下载的是Linux版的chromedriver

连接的Python代码如下

Python
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "192.168.1.10:19222")
c = webdriver.Chrome(executable_path='chromedriver', chrome_options=chrome_options)
c.get('https://www.google.com')

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]