【问题标题】:Why do I get ResourceWarning more on Edge than Chrome?为什么我在 Edge 上获得的 ResourceWarning 比在 Chrome 上更多?
【发布时间】:2020-05-08 20:33:55
【问题描述】:

我已经和我的团队一起进行跨浏览器测试已经有一段时间了,我已经整理了我的 Chrome 测试。

所以今天我决定尝试运行我队友的 Edge 测试,我收到了 ResourceWarning: Enable tracemalloc to get the object allocation traceback 消息,就像疯了一样。大约每秒左右它会打印出 1 到 3 条相同的消息。

我记得在开始时运行 Chrome 测试时偶尔会收到此消息,但从来没有像我从 Edge 得到的那样多。

我做了一些小的研究(123)所以我知道这不会影响我的 Selenium 运行。

但我很好奇发生了什么幕后的事情,导致 Edge 比 Chrome 更容易发生这种情况。

有关我的 Edge 驱动程序的具体信息,我使用的是 Microsoft 网站上的 Edge 版本 80.0.361.66。我确实做了一些配置测试,看看是否有任何区别(驱动程序版本和边缘版本),但数量仍然相同。

这很奇怪,因为 Edge 使用 Chromium。我想知道这是否是 Selenium 如何控制 Edge 的问题。

这是 Chrome 的最低可行代码示例

import time
import unittest
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class test_state(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.driver = Chrome('__tests__/drivers/chromedriver.exe')
        self.driver.maximize_window()
        self.driver.get("https://adbanker.com/")

    def test_state(self):
        try:
            url = self.driver.current_url
            print(url)
            time.sleep(5)
            title = self.driver.title
            print(title)
            WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="for"]')))
            lr = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.ID,'statenav')))
            lr.click()
            ce = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="line_dropdown"]/option[3]')))
            ce.click()
            ca = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="state_dropdown"]/option[6]')))
            ca.click()
            time.sleep(5)
        except:
            raise


    @classmethod
    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

这是 Edge 的最小可行代码示例

import time
import unittest
from selenium.webdriver import Edge
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

class test_state(unittest.TestCase):
    @classmethod
    def setUp(self):
        self.driver = Edge('__tests__/drivers/msedgedriver.exe')
        self.driver.maximize_window()
        self.driver.get("https://adbanker.com/")

    def test_state(self):
        try:
            url = self.driver.current_url
            print(url)
            time.sleep(5)
            title = self.driver.title
            print(title)
            WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="for"]')))
            lr = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.ID,'statenav')))
            lr.click()
            ce = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="line_dropdown"]/option[3]')))
            ce.click()
            ca = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.XPATH,'//*[@id="state_dropdown"]/option[6]')))
            ca.click()
            time.sleep(5)
        except:
            raise


    @classmethod
    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

我一直在对此进行更多研究,特别是这个错误指向 Selenium 远程驱动程序中 this file 的第 374 行。

经过仔细检查,它在第 374 行遇到了一个 if 条件,只是不知道该怎么做,所以它失败了。但是为什么呢?

【问题讨论】:

  • 不知道完整的答案,但 c 中的“malloc”正在抓取一定大小的内存分配。那么 trace malloc 可能会返回那些内存分配的地址?您使用的是原始 Edge 还是 Edgium? Edgium(仍然命名为 Edge)是 edge,但它现在使用的是 chrome 引擎,如 google chrome。
  • 我正在使用 Edgium。我忘记将其标记为 Python,但您了解 trace malloc 的作用。它用于确定为对象分配内存的位置。导入后,您可以使用它来查找内存泄漏并比较某些点的内存快照。
  • 你能像Minimal, Complete, and Verifiable example那样发布足够的代码来重现问题吗?可能你正在使用 tracemalloc 模型来跟踪 Python 分配的内存块,或者如this link 所说,你忘记关闭文件了。
  • @ZhiLv-MSFT 按要求添加。即使在这个简单的示例中,我也会收到重要的 ResourceWarnings。很奇怪。

标签: python selenium selenium-chromedriver microsoft-edge


【解决方案1】:

我已经测试了你的代码,它会显示以下错误:“ResourceWarning: unclosed”

我们可以尝试使用以下代码忽略此警告:

Python 3 脚本:

if __name__ == '__main__':
    unittest.main(warnings='ignore')

更多详细信息,请查看Python 3 unittest docs

另外,您也可以参考this thread关闭资源。

【讨论】:

  • 我现在已经忽略了警告,但我真的很担心找出为什么这个问题在 Edge 而不是我正在测试的任何其他浏览器上普遍存在。具体来说,我收到的所有警告都指向 RemoteConnection selenium 代码的第 374 行。
  • 根据official docs,这个警告与资源使用有关。在检查了一些带有类似警告的类似线程后,似乎出现这个警告是因为Requests使用了keep-alive模型,这意味着我们尝试在许多情况下不要显式关闭套接字。发生这种情况的确切原因可能是由于您使用了在测试结束时未明确关闭的 Session 对象,或者使用了较旧版本的 Requests。由于此警告通常不会终止程序,因此解决方法是忽略此警告。
【解决方案2】:

升级到msedge v81,使用selenium==4.0.0a5,需要用到的地方:

    options = EdgeOptions()
    #options.add_argument('headless')
    #options.add_argument('disable-gpu')
    options.add_argument('log-level=0')
    options.use_chromium = True
    #options.binary_location = r"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
    self.driver = Edge(options=options)

在较旧的 selenium 上,Edge() 中没有 use_chromium 键。它比 Chrome 慢,并且会将大量调试信息转储到标准输出中。

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 2013-07-12
    • 2019-04-04
    • 2011-06-28
    • 2014-04-13
    • 2013-08-18
    • 2021-03-08
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多