一次失败的Selenium chromedriver切换

背景

Selenium webdriver一直使用Firefox作为浏览器来跑webtest, 但是最近发现ff有时会报超时的错误,于是想到使用chromedriver来提升稳定性。本想只把.firefox() 换成 .chrome() 这么简单的事情,结果却引出很多问题。

做法

根据官方文档

  1. 下载chromedriver binary
  2. 放到Linux 默认路径
  3. 走起…
  4. 问题来了

问题

Selenium 报无法启动chrome, 报错

selenium.common.exceptions.WebDriverException: Message: chrome not reachable
  (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 3.0.36-gentoo x86_64)

排错

  1. 用本机来跑, 排除Selenium Grid的因素
  2. 更新Selenium和webdriver 到最新
  3. 添加 –no-sandbox 参数
  4. 还是跪了,用最小的测试依赖来跑,并生成chromedriver log,把问题丢给google chrome 团队了
    test_chrom_min.py
from selenium import webdriver

service_log_path = 'chromedriver.log'
service_args = ['--verbose', '--no-sandbox']

driver = webdriver.Chrome('/usr/bin/chromedriver', service_args=service_args,
        service_log_path=service_log_path) 
driver.get('http://www.google.com/xhtml')
driver.quit()

后续

  1. 在Mac上可以成功跑通,估计是测试环境上的问题
  2. Reported Bug https://bugs.chromium.org/p/chromedriver/issues/detail?id=397#c43
原文地址:https://www.cnblogs.com/liangnote/p/5190445.html