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

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

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,然后以远程调试模式运行

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端口

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代码如下

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')