1. IE浏览器,需要配合下载IEDriverSever.exe的驱动程序,目前selenium支持IE9以上。
(驱动程序下载链接:https://pan.baidu.com/s/1YpaUsIs128znSOBQmHdzWw 密码: mxfq)。
访问搜狗主页的脚本:
#VisitSogouByIE.py 访问搜狗主页例子 #encoding=utf-8 from selenium import webdriver import unittest class VisitSogouByIE(unittest.TestCase): def setUp(self): # 启动IE浏览器 self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer") def test_visitSogou(self): # 访问搜索首页 self.driver.get("http://www.sogou.com") # 打印当前网页的网址 print self.driver.current_url def tearDown(self): # 退出IE浏览器 self.driver.quit() if __name__ == '__main__': unittest.main()
#encoding=utf-8
from selenium import webdriver
import unittest
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
# 启动IE浏览器
self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
def test_visitSogou(self):
# 访问搜索首页
self.driver.get("http://www.sogou.com")
# 打印当前网页的网址
print self.driver.current_url
def tearDown(self):
# 退出IE浏览器
self.driver.quit()
if __name__ == '__main__':
unittest.main()
2. Firefox浏览器,需要配合下载geckoDriver.exe的驱动程序
(驱动程序下载地址:https://pan.baidu.com/s/16X-dRmSzrUx-r1rIaCnQFw 密码: ra79)
访问搜狗主页的脚本:
#encoding=utf-8 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium import webdriver import unittest class VisitSogouByFirefox(unittest.TestCase): def setUp(self): #binary = FirefoxBinary('D:\FirefoxPortable\Firefox.exe') # 启动Firefox浏览器 self.driver = webdriver.Firefox(executable_path = "e:\geckodriver") #driver = webdriver.Firefox(firefox_binary = binary,executable_path = r"c:geckodriver") def test_visitSogou(self): # 访问搜索首页 self.driver.get("http://www.sogou.com") # 打印当前网页的网址 print self.driver.current_url def tearDown(self): # 退出firefox浏览器 self.driver.quit() if __name__ == '__main__': unittest.main()
3. Firefox浏览器,需要配合下载 chromedriver.exe的驱动程序
(驱动程序下载地址:https://pan.baidu.com/s/1Sei0ZkcjNYBsdNUKPEgKYg 密码: br6s)
访问搜狗主页的脚本:
#encoding=utf-8 from selenium import webdriver import unittest class VisitSogouByChrome(unittest.TestCase): def setUp(self): # 启动Chrome浏览器 self.driver = webdriver.Chrome(executable_path = "E:\chromedriver") def test_visitSogou(self): # 访问搜索首页 self.driver.get("http://www.sogou.com") # 打印当前网页的网址 print self.driver.current_url def tearDown(self): # 退出IE浏览器 self.driver.quit() if __name__ == '__main__': unittest.main()