错误信息:Passthrough is not supported, GL is swiftshader
解决方案:通过ChromeOptions()
配置Selenium启动参数进而实现chromedriver
在Windows环境中以“无头模式”运行时于控制台的输出信息的控制:
from selenium.webdriver import Chrome, ChromeOptions
# True:chromedriver静默启动
SILENCE = True
def set_spider_option(chromedriver_path=None) -> Chrome:
# 调整chromedriver的读取路径,若不指定则尝试从环境变量中查找
chromedriver_path = "chromedriver" if chromedriver_path is None else chromedriver_path
# 实例化Chrome可选参数
options = ChromeOptions()
# 静默启动 参数组策略
if SILENCE is True:
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--disable-software-rasterizer")
# 其他推荐设置
options.add_argument('--log-level=3')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('excludeSwitches', ['enable-automation'])
return Chrome(options=options, executable_path=chromedriver_path)
def this_is_a_business(chromedriver_path):
api = set_spider_option(chromedriver_path)
try:
api.get("https://www.baidu.com")
input()
finally:
api.quit()
if __name__ == '__main__':
this_is_a_business(chromedriver_path=None)
参考资料
[2] python - Passthrough is not supported, GL is disabled - Stack Overflow