python selenium库安装,驱动安装,设置无界面模式

库安装

安装selenium库

pip3 install selenium

驱动安装

运行python代码时报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

解决:

安装与浏览器版本匹配的webdriver

1、打开谷歌浏览器, 在地址栏输入 chrome://version/ 查看版本信息:

 2、选择合适版本的驱动下载:

下载地址:http://chromedriver.storage.googleapis.com/index.html

 3.解压下载的驱动放到指定目录,代码调用时指定该目录即可。
解压后的目录:

将chromedriver.exe 放入安装的selenium库下

 之后,将该驱动路径加入到代码中即可:

from selenium import webdriver
chrome_driver=r"D:\Python\Anaconda\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chrome_driver)

 

设置无界面模式

from selenium import webdriver                                                       
chrome_options = webdriver.ChromeOptions()                                           
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu') #如果在Windows上运行,还需要包含该标志                                      
driver = webdriver.Chrome(chrome_options=chrome_options)                             
driver.get('http://www.baidu.com')                                                   
driver.save_screenshot('/Users/hnbl009/gitfile/webtest/pyhon_zh_web/logs/ceshi.png') 

 chrome 

--headless \ # Runs Chrome in headless mode.
--disable-gpu \ # Temporarily needed if running on Windows.

猜你喜欢

转载自www.cnblogs.com/pfeiliu/p/12275239.html