先看代碼(只是一段代碼):
from selenium import webdriver class Register(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.example.com/" self.verificationErrors = [] self.accept_next_alert = true
上面這段代碼運行的時候,selenium會打開一個新的,不含有任何插件和個人證書的firefox(等同於全新安裝后第一次打開的那個firefox)。
但是有些時候,我們希望selenium打開的firefox要有插件或者有已經導入的個人證書,這種情況下,上面的代碼就不起作用了。
這時候,我們就會用到firefoxprofile。
首先,介紹一下FirefoxProfile。
要了解Firefox profile請訪問這里,它詳細解紹了Firefox proflie。在Firefox里,如何管理Firefox profile 請訪問這里。
既然已經了解過Firefox profile,那么來解決我上面提出的問題。
其實上面的問題很簡單,就是使用selenium啟動平時使用的Firefox,而不讓系統去啟動一個新的什么都沒有的瀏覽器。
代碼如下:
from selenium import webdriver class Register(unittest.TestCase): def setUp(self): self.profileDir = "\path\your\firefoxprofileDir" self.profile = webdriver.FirefoxProifle(self.profileDir) self.driver = webdriver.Firefox(self.profile) self.driver.implicitly_wait(30) self.base_url = "https://www.example.com/" self.verificationErrors = []
首先,我先定義我需要使用的profile的文件夾,然后創建一個profile,並且把profile的地址傳給它,接着啟動firefox的時候傳入這個profile,這樣,系統再打開的firefox就是我們平時使用的那個了。