Selenium Headless chrome运行速度慢得多

wecizke3  于 2023-04-03  发布在  Go
关注(0)|答案(3)|浏览(305)

我有一个Selenium解析器的类:

class DynamicParser(Parser):
    """Selenium Parser with processing JS"""
    driver: Chrome = None

    def __init__(self, driver_path='./chromedriver', headless=True):
        chrome_options = Options()
        if headless:
            chrome_options.add_argument("--headless")
            chrome_options.add_argument("window-size=1920,1080")

        # bypass OS security
        chrome_options.add_argument('--no-sandbox')
        # overcome limited resources
        chrome_options.add_argument('--disable-dev-shm-usage')
        # don't tell chrome that it is automated
        chrome_options.add_experimental_option(
            "excludeSwitches", ["enable-automation"])
        chrome_options.add_experimental_option('useAutomationExtension', False)
        # disable images
        prefs = {"profile.managed_default_content_settings.images": 2}
        chrome_options.add_experimental_option("prefs", prefs)

        # Setting Capabilities
        capabilities = DesiredCapabilities.CHROME.copy()
        capabilities['acceptSslCerts'] = True
        capabilities['acceptInsecureCerts'] = True

        self.driver = Chrome(chrome_options=chrome_options,
                             executable_path=driver_path, desired_capabilities=capabilities)

    def goto(self, url: str):
        """Goes to specified URL"""
        self.driver.get(url)

    def get_seller_name(self) -> str:
        """Returns seller's name"""
        offer_actions_tag = self.driver.find_element_by_class_name(
            'offer-user__actions')
        profile_link_tag = offer_actions_tag.find_element_by_tag_name('a')
        return profile_link_tag.text.strip()

我还有一个测试脚本,它创建DynamicParser,转到某个页面并调用.get_seller_name()
我注意到,当我运行Chromedriver headless时,它的运行速度要慢得多,所以我使用time python3 test.py进行了测试。
无头镀 chrome 的输出:

python3 test.py  2,98s user 0,94s system 3% cpu 2:04,65 total

非无头镀 chrome 的输出:

python3 test.py  1,48s user 0,33s system 47% cpu 3,790 total

正如我们所看到的,无头chrome运行速度慢了近33倍!
Chrome版本:83.0.4103.116
Chrome驱动程序版本:83.0.4103.39
我真的不明白是什么问题,当我开发我以前的应用程序时,无头chrome运行得足够快。

e0bqpujr

e0bqpujr1#

刚发现问题所在是

chrome_options.add_argument('--disable-dev-shm-usage')

我以为它应该有无限的 chrome 资源,但它肯定不会在这种情况下工作。

j9per5c4

j9per5c42#

当运行无头驱动程序时,您也可以使用这些设置来提高性能。

browser_options = webdriver.ChromeOptions()
browser_options.headless = True
image_preferences = {"profile.managed_default_content_settings.images": 2}
browser_options.add_experimental_option("prefs", image_preferences)
h5qlskok

h5qlskok3#

我发现这些对我都不起作用。
然而,将options.add_argument('--headless')更改为options.add_argument('--headless=new')产生了巨大的差异,似乎完全解决了这个问题。

相关问题