1. 程式人生 > >瀏覽器開啟載入外掛啟動

瀏覽器開啟載入外掛啟動

使用者配置檔案建立方法:

1.在CMD中使用cd命令進入firefox.exe檔案所在目錄(比如:C:\Program Files\Mozilla Firefox),

並輸入firefox.exe -ProfileManager -no-remote命令,然後按Enter鍵,

調出“Firefox – 選擇使用者配置檔案”操作視窗

2.如果firefox.exe -ProfileManager -no-remote 執行彈出一個頁面說找不到路徑,解決方法:

在火狐的選單“幫助”下,選擇“故障排除資訊”,點選後,在彈出的頁面中找到“配置資料夾 ”的

選項,點選“開啟資料夾”,可以獲取預設配置檔案的全路徑。

3.進入mac的火狐路徑:/Applications/Firefox.app/Contents/MacOS

執行:firefox -profilemanager

新建profile

程式碼示例

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time

class TestFailCaptureScreen(unittest.TestCase):
def setUp(self):
    # 建立儲存自定義配置檔案的路徑變數
    #proPath = "C:\\Users\\wuxiaohua\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\tbbmxtkv.webdriver"
    proPath = "C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\0cpot01t.default"
    # 載入自定義配置檔案到FirefoxProfile例項中,
    # 等價profile = webdriver.FirefoxProfile(proPath)
    profile = webdriver.firefox.firefox_profile.FirefoxProfile(proPath)
    # 將添加了新配置檔案的Firefox瀏覽器首頁設為搜狗主頁
    profile.set_preference("browser.startup.homepage", "http://www.sogou.com")
    # 設定開始頁面不是空白頁,0表示空白頁,
    # 這一步必須做,否則設定的主頁不會生效
    profile.set_preference("browser.startup.page", 1)
    # 啟動帶自定義配置檔案的Firefox瀏覽器
    self.driver = webdriver.Firefox(executable_path="c:\\geckodriver", firefox_profile=profile)

def testSoGouSearch(self):
    # 等待5秒,以便瀏覽器啟動完成
    time.sleep(5)
    try:
        # 找到搜狗主頁搜尋輸入框頁面元素
        searchBox = self.driver.find_element_by_id("query")
        # 在找到的搜尋輸入框中輸入“光榮之路自動化測試”
        searchBox.send_keys(u"光榮之路自動化測試")
        # 找到搜尋按鈕,並點選
        self.driver.find_element_by_id("stb").click()
        time.sleep(10)
    except NoSuchElementException, e:
        print "修改帶自定義配置檔案的瀏覽器主頁不成功!"

def tearDown(self):
    # 退出IE瀏覽器
    self.driver.quit()

if __name__ == '__main__':
    unittest.main()`