博文章节
博文章节本机环境介绍Selenium 3x对于Chrome启动的改变Selenium 3x对于Firefox启动的改变Selenium 3x对于IE启动的改变拓展延伸本机环境介绍
编码语言(Python)
(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>python -VPython 3.6.2
Selenium版本(3.7.0)
(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>pip show seleniumName: seleniumVersion: 3.7.0Summary: Python bindings for Selenium
Selenium 3.x对于Chrome启动的改变
from selenium import webdriverif __name__ == '__main__':driver = webdriver.Chrome()driver.get("/")print(driver.title())
如果使用以上代码,将会有如下报错信息:
Traceback (most recent call last):File "C:/Users/XXX/iCloudDrive/PycharmProjects/SAT/sat/browsers/chrome_launcher.py", line 4, in <module>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 startos.path.basename(self.path), self.start_error_message)mon.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see /a//chromedriver/home
提示chromedriver
没有加入到环境变量PATH
中,chromedriver
下载地址,注意下载与自己浏览器相匹配版本的chromedriver。修改代码如下:
from selenium import webdriverif __name__ == '__main__':chrome_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"driver = webdriver.Chrome(chrome_driver_path)driver.get("/")print(driver.title)driver.quit()
Selenium 3.x对于Firefox启动的改变
from selenium import webdriverif __name__ == '__main__':driver = webdriver.Firefox()driver.get("/")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 <module>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 startos.path.basename(self.path), self.start_error_message)mon.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
提示geckodriver
没有加入到环境变量PATH
中,geckodriver
下载地址,注意下载与自己浏览器相匹配版本的geckodriver。修改代码如下:
from selenium import webdriverif __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("/")print(driver.title)driver.quit()
如果以上代码提示Unable to find a matching set of capabilities
错误,检查Firefox版本与geckodriver版本的匹配即可
Selenium 3.x对于IE启动的改变
from selenium import webdriverif __name__ == '__main__':driver = webdriver.Ie()driver.get("/")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 <module>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_sessionresponse = self.execute(Command.NEW_SESSION, parameters)File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in executeself.error_handler.check_response(response)File "C:\Virtualenv\py3env\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_responseraise exception_class(message, screen, stacktrace)mon.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 webdriverif __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("/")print(driver.title)driver.quit()
拓展延伸
python selenium 3.7 API Docs 官方地址