安装了selenium后,直接用Firefox浏览器即可运行,如果要使用别的浏览器chrome、ie等运行python脚本,需要下载相应的驱动文件:
下面以chrome为例,介绍几种形式运行chrome浏览器
(chromedriver下载地址:https://sites.google.com/a/chromium.org/chromedriver/)
-
方式一
- 下载chromedriver驱动,将该文件解压后,拷贝到路径/Users/xxxxxx/files/python/selfPractise/Test_framework/drivers下
(python脚本所在路径是:/Users/xxxxxx/files/python/selfPractise/Test_framework/test/case/test_baidu_2.py) - 启动chrome浏览器脚本:
driver = webdriver.Chrome(executable_path=DRIVER_PATH + '/chromedriver') #其中executable_path等号右边的路径即是chromedriver的所在路径
driver.get(URL)
-
方式二:
- 将chromedriver拷贝到/Users/xxxxxx/anaconda/bin/下面
- 启动chrome浏览器脚本:
_chrome_options = Options()
_chrome_options.add_argument('disable-infobars')
self.driver = webdriver.Chrome()
self.driver.get(self.URL)
-
方式三:
和方式一类似,只是python脚本做了下优化
- 下载chromedriver驱动,将该文件解压后,拷贝到路径/Users/xxxxxx/files/python/selfPractise/Test_framework/drivers下
(python脚本所在路径是:/Users/xxxxxx/files/python/selfPractise/Test_framework/test/case/test_baidu_2.py) - 启动chrome浏览器脚本:
_chrome_options = Options()
_chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path=DRIVER_PATH + '/chromedriver',chrome_options=_chrome_options) #其中executable_path等号右边的路径即是chromedriver的所在路径
driver.get(URL)
网友评论