二 Selenium之启动浏览器
一 配置浏览器的驱动
friefox驱动下载地址:https://github.com/mozilla/geckodriver/releases
ie 下载地址: http://selenium-release.storage.googleapis.com/index.html 请从中选择最新版,注意是32位还是64位。
chrome 下载地址:http://chromedriver.storage.googleapis.com/index.html 请从中选择最新版,注意是32位还是64位。
phantomjs 下载地址:http://phantomjs.org/download.html 请从中选择最新版,注意是32位还是64位。
注:① 请注意各驱动所支持的对应的浏览器版本(webdriver、驱动、浏览器三者需匹配),不然会出现启动浏览器失败或connect timeout等异常;
②要使用geckodriver,须把selenium升级至3.3及以上版本 ;
③,建议直接解压到Python安装目录下,如D:/Python
二 启动谷歌浏览器
查看谷歌的一些配置:https://www.cnblogs.com/xmlbw/p/4498113.html
from selenium import webdriver import os #返回path规范化的绝对路径 driver_path = os.path.abspath('D:\\chromedriver.exe') os.environ['webdriver.chrome.driver'] = driver_path print(driver_path) driver = webdriver.Chrome(driver_path) driver.get("https://www.baidu.com")
from selenium import webdriver # Create your tests here. options = webdriver.ChromeOptions() #屏蔽“Chrome正受到自动测试软件的控制”提示信息 options.add_argument('disable-infobars') driver = webdriver.Chrome(chrome_options=options) driver.get("https://www.baidu.com") driver.close()
注意: 谷歌浏览器新版本中已经废弃了disable-infobars,可用如下方法屏蔽'受自动化软甲的控制'
options = webdriver.ChromeOptions() #屏蔽“Chrome正受到自动测试软件的控制”提示信息 options.add_experimental_option("excludeSwitches", ['enable-automation'])
chrome59版本开始增加了Chrome-headless模式,可以无界面运行浏览器,瞬间抢走了PhantomJS的饭碗
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Create your tests here. options = webdriver.ChromeOptions() #开启谷歌无界浏览器模式,Chrome-headless模式 options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options) driver.get("https://www.baidu.com") print(driver.title) #退出driver关闭浏览器 driver.quit()
三 启动火狐浏览器
from selenium import webdriver import os driver_path = os.path.abspath(r'D:\geckodriver.exe') driver = webdriver.Firefox(executable_path=driver_path) driver.get("https://www.baidu.com")
from selenium import webdriver import os driver = webdriver.Firefox() driver.get("https://www.baidu.com")
四 启动Edge浏览器
from selenium import webdriver import os driver_path = os.path.abspath(r"D:\MicrosoftWebDriver.exe") driver = webdriver.Edge(driver_path) driver.get("https://www.baidu.com")
from selenium import webdriver import os driver = webdriver.Edge() driver.get("https://www.baidu.com")
浙公网安备 33010602011771号