[英]Can't capture HAR using Python Selenium Script with BrowserMob-Proxy
[英]Python BrowserMob Proxy with IE captures incorrect HAR?
我目前正在嘗試使用適用於Python(v2.6)的BrowserMob代理(v2.1.1)+ Selenium(v2.5.3)來測試頁面加載時間並將其輸出到HAR文件。 我需要同時測試Chrome和IE。 我目前擁有適用於Chrome的完美瀏覽器,並且可以在IE上正常運行,但無法將正確的數據捕獲到HAR文件中。
此圖像是我得到的兩個不同的HAR文件的比較。 第一個是IE的結果,第二個是Chrome的結果。 我需要兩者都一樣。 我覺得我設置代理的方式有問題,但是根據http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp ,對於Chrome / IE來說基本上應該是同一件事我有 我的想法是它沒有使用正確的代理端口或其他東西,但是我不知道如何解決它
如您所見,它似乎也正在捕獲硒在頁面上的作用,這不是我想要的。 這是我正在使用的代碼:
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!"
在啟動瀏覽器之前,您需要確保IE的緩存是干凈的:
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER
desired_capabilities ["ie.ensureCleanSession"] = True
driver = webdriver.Ie(capabilities=desired_capabilities )
請注意,您正在讀取代理上的指標。 因此,您只測量每個請求的響應時間,而不是頁面加載時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.