Selenium不再推荐使用PhantomJS,会报如下警告
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
于是从PhantomJS转移到Chrome,使用headless versions of Chrome时,首先要安装Chrome,然后下载chromedriver,再把chromedriver的地址配置到系统环境变量path中,方便调用。如果不把chromedriver的地址配置到系统环境变量的话,也可以在使用时指定chromedriver的地址。
注意Chrome和chromedriver有版本对应的要求,系统中安装了某一版本的chrome要使用对应版本的chromedriver,其实下载最新版本的Chrome和chromedriver就行了,一般都是对应的。
Chrome下载地址:https://chrome.en.softonic.com/
chromedriver下载地址:http://npm.taobao.org/mirrors/chromedriver/
不过在使用过程中,还是不如PhantomJS顺手,在windows环境下,chrome还有点问题。比如启动chromedriver的时候,会报个错。
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') # webdriver.Chrome函数里有个参数executable_path,默认值是'chromedriver.exe',会搜索系统path变更,也可以修改该值,使用其他路径 # driver = webdriver.Chrome(executable_path=my_driver_path,chrome_options=chrome_options) driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("https://www.baidu.com/")
[0917/002914.533:ERROR:gpu_process_transport_factory.cc(1007)] Lost UI shared context.
虽然最终没有影响程序执行,但还是费了很大劲去查了一下,原因是在windows系统中Chrome无头模式下,其中的SwiftShader软件会触发断言失败,但实际上不影响程序执行,可以忽略该错误。
https://stackoverflow.com/questions/50143413/errorgpu-process-transport-factory-cc1007-lost-ui-shared-context-while-ini
来源:oschina
链接:https://my.oschina.net/u/3905482/blog/2254510