【问题标题】:Python BrowserMob Proxy with IE captures incorrect HAR?带有 IE 的 Python BrowserMob 代理捕获不正确的 HAR?
【发布时间】:2016-11-04 04:19:32
【问题描述】:

我目前正在尝试使用 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 来说应该基本上是一样的,就像我拥有它一样。我的想法是它没有使用正确的代理端口或其他东西,但我不知道如何修复它

如您所见,它似乎也在捕捉 selenium 在页面上所做的事情,这不是我想要的。这是我正在使用的代码:

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!"

【问题讨论】:

    标签: python internet-explorer selenium browsermob-proxy


    【解决方案1】:

    在启动浏览器之前需要确保IE的缓存是干净的:

    desired_capabilities  = webdriver.DesiredCapabilities.INTERNETEXPLORER
    desired_capabilities ["ie.ensureCleanSession"] = True
    driver = webdriver.Ie(capabilities=desired_capabilities )
    

    请注意,您正在阅读代理上的指标。因此,您只测量每个请求的响应时间,而不是页面加载时间。

    【讨论】:

    • 这解决了它。非常感谢;它开始让我头疼。出于好奇,Chrome 驱动程序是否会自动清除缓存,这就是 Chrome 没有问题的原因?
    • Chrome 驱动程序在每次启动时都会创建一个临时配置文件,因此它总是以干净的会话开始。
    • 啊,现在更有意义了。感谢大家的帮助!
    • 作为最新的 'iedriverserver.exe' (3.8.0) 的更新,修改了所需功能 Dict 的结构,现在嵌套了 'ie.ensureCleanSession' 变量。为了解决这个问题,初始化包装字典并设置变量如下: caps = DesiredCapabilities.INTERNETEXPLORER caps['se:ieOptions'] = {} caps['se:ieOptions']['ie.ensureCleanSession '] = True caps['ignoreZoomSetting'] = True self.driver = webdriver.Ie(capabilities=caps)
    猜你喜欢
    • 2014-11-19
    • 2016-08-19
    • 1970-01-01
    • 2019-10-09
    • 2019-02-02
    • 2017-08-22
    • 2023-03-29
    • 2016-09-10
    • 2018-11-06
    相关资源
    最近更新 更多