2016-07-01 43 views
0

我目前正在嘗試爲Python(v2.6)使用BrowserMob Proxy(v2.1.1)+ Selenium(v2.5.3)來測試頁面加載時間並將它們輸出到HAR文件。我需要測試Chrome和IE。我目前對Chrome非常適用,它在IE上運行時沒有錯誤,但不會將正確的數據捕獲到HAR文件中。使用IE的Python BrowserMob Proxy捕獲不正確的HAR?

此圖像是我得到的兩個不同HAR文件的比較。第一個是IE的結果,第二個是Chrome的結果。我需要爲兩者獲得相同的結果。我有一種感覺,我在設置代理的方式上做錯了事,但根據http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp,Chrome/IE應該基本上與我擁有的一樣。我的想法是,它的使用不正確的代理端口或東西,但我不知道如何解決它

enter image description here

正如你所看到的,似乎也捕捉什麼硒是做對這不是我想要的頁面。這是我使用的代碼:

class SeleniumObject: 

    def __init__(self): 
     # Start up the server 
     self.server = Server(Config.BAT_PATH) #resolves to the location of browsermob-proxy-2.1.1/bin/browsermob-proxy.bat 
     self.server.start() 
     self.proxy = self.server.create_proxy() 

    def setupDriver(self, browser): 
     self.browser = browser.lower() 
     PROXY = self.proxy.proxy 

     # Chrome 
     if self.browser == 'chrome': 
      options = webdriver.ChromeOptions() 
      options.add_argument("--start-maximized") 
      desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy() 
      # Change the proxy properties of that copy. 
      desired_capabilities['proxy'] = { 
       "httpProxy":PROXY, 
       "ftpProxy":PROXY, 
       "sslProxy":PROXY, 
       "noProxy":None, 
       "proxyType":"MANUAL", 
       "class":"org.openqa.selenium.Proxy", 
       "autodetect":False 
      } 
      self.driver = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_capabilities) 

     # IE 
     if self.browser == 'ie': 
      desired_capabilities = webdriver.DesiredCapabilities.HTMLUNITWITHJS.copy() 
      desired_capabilities['proxy'] = { 
       "httpProxy":PROXY, 
       "ftpProxy":PROXY, 
       "sslProxy":PROXY, 
       "noProxy":None, 
       "proxyType":"MANUAL", 
       "class":"org.openqa.selenium.Proxy", 
       "autodetect":False 
      } 
      self.driver = webdriver.Ie(capabilities=desired_capabilities) 

    def outputHAR(self): 
      # Output the data as a HAR file 
      self.har_json = json.dumps(self.proxy.har, indent=4, sort_keys=True) # returns a HAR JSON blob 
      open(self.browser + '-load-summary-' + self.sample_time + '.har', 'w').write(self.har_json) 

    def setSampleTime(self): 
     self.sample_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") 

    def shutDown(self): 
     self.setRegKey("ProxyEnable", 0) # Forces the internet setting to stop using the proxy in case there was an error 
     self.driver.quit() 
     self.proxy.close() 
     self.server.stop() 

selenium = SeleniumObject() 
selenium.setupDriver("chrome") 
selenium.setSampleTime() 
selenium.proxy.new_har("W3Schools") 
selenium.driver.get("http://www.w3schools.com") 
selenium.outputHAR() 
selenium.shutDown() 
print "Done!" 

回答

1

您需要確保IE的緩存是在啓動瀏覽器之前清潔:

​​

請注意,您正在閱讀的代理指標。因此,您只是測量每個請求的響應時間,而不是頁面加載時間。

+0

這解決了它。非常感謝;它開始讓我頭疼。出於好奇,Chrome驅動程序是否會自動清除緩存,這就是爲什麼它不是Chrome的問題? –

+0

Chrome驅動程序在每次啓動時都會創建一個臨時配置文件,因此它始終以乾淨的會話開始。 –

+0

啊,那現在變得更有意義了。感謝所有的幫助! –

相關問題