Selenium 3.x如何启动3种浏览器(Chrome/Firefox/IE)

博文章节

  • 博文章节
    • 本机环境介绍
    • Selenium 3x对于Chrome启动的改变
    • Selenium 3x对于Firefox启动的改变
    • Selenium 3x对于IE启动的改变
    • 拓展延伸

本机环境介绍

编码语言(Python)

(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>python -V
Python 3.6.2

Selenium版本(3.7.0)

(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>pip show selenium
Name: selenium
Version: 3.7.0
Summary: Python bindings for Selenium

Selenium 3.x对于Chrome启动的改变

from selenium import webdriver

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com/")
    print(driver.title())

如果使用以上代码,将会有如下报错信息:

Traceback (most recent call last):
  File "C:/Users/XXX/iCloudDrive/PycharmProjects/SAT/sat/browsers/chrome_launcher.py", line 4, in 
    driver = webdriver.Chrome()
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
    self.service.start()
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

提示chromedriver没有加入到环境变量PATH中,chromedriver下载地址,注意下载与自己浏览器相匹配版本的chromedriver。修改代码如下:

from selenium import webdriver

if __name__ == '__main__':
    chrome_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get("https://www.baidu.com/")
    print(driver.title)
    driver.quit()

Selenium 3.x对于Firefox启动的改变

from selenium import webdriver

if __name__ == '__main__':
    driver = webdriver.Firefox()
    driver.get("https://www.baidu.com/")
    print(driver.title)
    driver.quit()

如果使用以上代码,将会有如下报错信息:

Traceback (most recent call last):
  File "C:/Users/XXX/iCloudDrive/PycharmProjects/SAT/sat/browsers/firefox_launcher.py", line 4, in 
    driver = webdriver.Firefox()
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in __init__
    self.service.start()
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

提示geckodriver没有加入到环境变量PATH中,geckodriver下载地址,注意下载与自己浏览器相匹配版本的geckodriver。修改代码如下:

from selenium import webdriver

if __name__ == '__main__':
    firefox_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\geckodriver.exe"
    driver = webdriver.Firefox(executable_path=firefox_driver_path)
    driver.get("https://www.baidu.com/")
    print(driver.title)
    driver.quit()

如果以上代码提示Unable to find a matching set of capabilities错误,检查Firefox版本与geckodriver版本的匹配即可

Selenium 3.x对于IE启动的改变

from selenium import webdriver

if __name__ == '__main__':
    driver = webdriver.Ie()
    driver.get("https://www.baidu.com/")
    print(driver.title)
    driver.quit()

如果使用以上代码,将会有如下报错信息:

Traceback (most recent call last):
  File "C:/Users/XXX/iCloudDrive/PycharmProjects/SAT/sat/browsers/firefox_launcher.py", line 5, in 
    driver = webdriver.Firefox(executable_path=chrome_driver_path)
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 154, in __init__
    keep_alive=True)
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 151, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 240, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities

提示Unable to find a matching set of capabilities,通过Google发现自Selenium3.0开始IE启动需要加载IEDriverServer.exe,InternetExplorerDriver介绍,下载地址。修改代码如下:

from selenium import webdriver

if __name__ == '__main__':
    ie_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\IEDriverServer.exe"
    driver = webdriver.Ie(executable_path=ie_driver_path)
    driver.get("https://www.baidu.com/")
    print(driver.title)
    driver.quit()

拓展延伸

python selenium 3.7 API Docs 官方地址

你可能感兴趣的:(自动化测试)