練習啓動各類瀏覽器的同時加載插件:Firefox, Chrome, IE

# -*- coding:utf-8 -*-
import os
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keyspython

"""
練習啓動各類瀏覽器:Firefox, Chrome, IE
練習啓動各類瀏覽器的同時加載插件:Firefox, Chrome, IE
"""web

def startFirefox():
"""啓動安裝在默認位置的Firefox瀏覽器,並自動轉到 百度 首頁"""
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
assert("百度" in driver.title)
elem = driver.find_element_by_name("wd")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "百度" in driver.title
driver.close()
driver.quit()
driver = None

def startFirefoxWithSpecificLocation():
"""啓動安裝在 非 默認位置的Firefox瀏覽器,並自動轉到 百度 首頁"""
firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
os.environ["webdriver.firefox.bin"] = firefoxBin

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
assert("百度" in driver.title)
elem = driver.find_element_by_name("wd")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "百度" in driver.title
driver.close()
driver.quit()
driver = Nonechrome


def startChrome():
"""啓動Chrome瀏覽器,並自動轉到 百度 首頁
啓動Chrome瀏覽器須要指定驅動的位置
"""
chrome_driver = os.path.abspath(r"D:\Files\chromedriver.exe")
os.environ["webdriver.chrome.driver"] = chrome_driver

driver = webdriver.Chrome(chrome_driver)
driver.get("http://www.baidu.com")
assert("百度" in driver.title)
elem = driver.find_element_by_name("wd")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "百度" in driver.title
driver.close()
driver.quit()
driver = None瀏覽器

def startIE():
"""啓動IE瀏覽器,並自動轉到 百度 首頁
啓動 IE 瀏覽器須要指定驅動的位置
"""
ie_driver = os.path.abspath(r"D:\Files\IEDriverServer.exe")
os.environ["webdriver.ie.driver"] = ie_driver

driver = webdriver.Ie(ie_driver)
driver.get("http://www.python.org")
assert("Python" in driver.title)
elem = driver.find_element_by_id("id-search-field")
elem.send_keys("selenium")
'''
elem.send_keys(Keys.RETURN)
assert "百度" in driver.title
driver.close()
driver.quit()
driver = None
'''
ui

def start_firefox_with_firebug_plug():
"""啓動Firefox,並自動加載插件Firebug"""
firefoxBin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
os.environ["webdriver.firefox.bin"] = firefoxBin

firefoxProfile = webdriver.FirefoxProfile()
tempDir = os.getcwd()
tempDir = os.path.split(tempDir)[0]
firebugPlugFile = os.path.join(os.path.join(tempDir,"Files"), "firebug-2.0.7.xpi")
firefoxProfile.add_extension(firebugPlugFile)
firefoxProfile.set_preference("extensions.firebug.currentVersion", "2.0.7")

driver = webdriver.Firefox(firefox_profile=firefoxProfile)
driver.get("http://www.baidu.com")spa

def start_chrome_with_chrometomobile_plug():
"""啓動Chrome,並自動加載插件Chrome to Mobile"""
tempDir = os.getcwd()
tempDir = os.path.split(tempDir)[0]
chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
os.environ["webdriver.chrome.driver"] = chrome_driver_file

chrome_to_mobile_plug_file = os.path.join(os.path.join(tempDir,"Files"), "Chrome-to-Mobile_v3.3.crx")
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension(chrome_to_mobile_plug_file)
driver = webdriver.Chrome(executable_path=chrome_driver_file,
chrome_options=chrome_options)
driver.get("http://www.baidu.com")
'''
driver.close()
driver.quit()
driver = None
'''.net

def start_firefox_with_default_settings():
"""啓動Firefox瀏覽器, 使用本地配置文件中的選項配置瀏覽器
自動將頁面載入過程導出爲Har文件,並存放在
配置項 extensions.firebug.netexport.defaultLogDir指定的D:\temp\selenium2目錄下
"""
firefox_bin = os.path.abspath(r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
os.environ["webdriver.firefox.bin"] = firefox_bin

# 使用從別的機器上拷貝來的瀏覽器配置
firefox_profile = webdriver.FirefoxProfile(os.path.abspath(r"D:\Temp\selenium2\Profiles\mm9zxom8.default"))
# 使用本地的默認配置
#firefox_profile = webdriver.FirefoxProfile(r"C:\Users\eli\AppData\Roaming\Mozilla\Firefox\Profiles\mm9zxom8.default")
driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get("http://www.baidu.com")
driver.get("http://www.baidu.com")
'''
driver.close()
driver.quit()
driver = None
'''firefox

def start_chrome_with_default_settings():
"""啓動Firefox瀏覽器, 使用本地配置文件中的選項配置瀏覽器"""
tempDir = os.getcwd()
tempDir = os.path.split(tempDir)[0]
chrome_driver = chrome_driver_file = os.path.join(os.path.join(tempDir,"Files"), "chromedriver.exe")
os.environ["webdriver.chrome.driver"] = chrome_driver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--test-type")
chrome_options.add_argument("user-data-dir="+os.path.abspath(r"C:\Users\eli\AppData\Local\Google\Chrome\User Data\Default"))插件

這個地方具體的查找方法爲:用Chrome地址欄輸入chrome://version/,查看本身的「我的資料路徑」,而後在瀏覽器啓動時,調用這個配置文件utf-8

這裏面有個坑,就是獲取的配置爲:C:\Users\ghhy00010\AppData\Local\Google\Chrome\User Data\Default

不少資料都顯示爲:C:\Users\ghhy00010\AppData\Local\Google\Chrome\User Data,而後一運行就報錯,報的錯

UnboundLocalError: local variable 'driver' referenced before assignment,一直提示瀏覽器沒有定義。

driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)

driver.get("http://www.baidu.com") if __name__ == "__main__": # 2.啓動瀏覽器時自動加載插件, 如Firefox -> Firebug ; Chrome -> Chrome to Mobile # start_firefox_with_firebug_plug() # start_chrome_with_chrometomobile_plug() # start_firefox_with_default_settings() start_chrome_with_default_settings() # 1.啓動各類瀏覽器 #startFirefox() #startFirefoxWithSpecificLocation() #startChrome() #startIE()

相關文章
相關標籤/搜索