2013-04-30 158 views
4

我正在使用Selenium爲Web應用程序創建一些端到端測試。始終允許使用Selenium的Firefox中的地理位置

我在Python工作和使用Firefox司機

driver = webdriver.Firefox()

的問題是,使用HTML5地理位置,而且似乎每次我跑我的測試我的web應用程序,我要點擊「允許位置「在Firefox中彈出,使得我的測試不是自動化的。

有沒有辦法強制Selenium Firefox驅動程序始終允許地理定位而不提示?

+0

不能確定-proper-解決方案,當:

並使用它在我的Python代碼是這樣的:

@classmethod def setUpClass(cls): cls.binary = FirefoxBinary(FF_BINARY_PATH) cls.profile = FirefoxProfile() cls.profile.set_preference("geo.prompt.testing", True) cls.profile.set_preference("geo.prompt.testing.allow", True) cls.profile.set_preference('geo.wifi.uri', GEOLOCATION_PATH) cls.driver = Firefox(firefox_binary=cls.binary, firefox_profile=cls.profile) 

GEOLOCATION_PATH是我的路徑JSON文件彈出窗口會出現(即它不會出現意外),那麼你可以使用win32ole send_keys..ha來解決這個問題,並在彈出時點擊Enter鍵) – 2013-05-01 16:14:04

回答

0

我相信默認是使用新的匿名配置文件啓動Firefox。您可以使用-Dwebdriver.firefox.profile = whatever啓動selenium,其中「whatever」是啓動firefox -P時配置文件的名稱。

爲了確保有持續性的登錄和其他餅乾沒有的怪事:

  • 啓動Firefox與「火狐-P」
  • 選擇配置文件,你會用
  • 編輯啓動測試 - >首選項 - >對歷史隱私,選擇使用自定義設置
  • 告訴火狐保持餅乾直到「我關閉Firefox」
+0

是的,這是結束了工作。很好,我可以補充。 – 2014-01-16 03:41:27

-2

手動允許它一次就足夠了,然後允許python執行該任務嗎?

因爲你可以很容易地在Firefox中的網站屬性設置此允許: enter image description here

7

您可以強制瀏覽器未經允許請求返回一些預定義位置。

只需執行下面的JavaScript代碼:

"navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }" 

測試在Firefox和Chrome。

+0

這對我來說確實有效,它允許其餘的測試通過。但是,它僅僅繞過運行真正地理定位的測試的能力。 – 2013-11-18 12:58:18

+0

作品! (在Firefox上測試25.0.1) – 2013-11-28 16:13:12

0

這裏是從上面的答案之一更精緻的答案。在進行地理位置成功回調之前,這會增加一些超時,因爲通常情況下,JavaScript會以這樣的方式編寫,即地理位置座標不會在直到有一個週期返回到事件循環時纔可用。

這也允許通過Web控制檯來跟蹤欺騙。

SPOOF_LOCATION_JS = """ 
(function() { 
console.log('Preparing geo spoofing'); 
navigator.geolocation.getCurrentPosition = function(success) { 
    console.log("getCurrentPosition() called"); 
    setTimeout(function() { console.log("Sending out fake coordinates"); success({coords: {latitude: 50.455755, longitude: 30.511565}}); }, 500); 
}; 
console.log("Finished geospoofing")})(); 
""" 


browser.evaluate_script(SPOOF_LOCATION_JS.replace("\n", " ")) 
1

因爲我得出了同樣的問題,近3年後,這個問題被問,以上答案都不是讓我滿意。我喜歡展示我使用的解決方案。

所以我在this blog找到的答案。但是,如果你知道

{ 
    "status": "OK", 
    "accuracy": 10.0, 
    "location": { 
     "lat": 50.850780, 
     "lng": 4.358138, 
     "latitude": 50.850780, 
     "longitude": 4.358138, 
     "accuracy": 10.0 
    } 
} 
相關問題