Singleton Selenium Chromedriver randomly decides not to connect, What could be the culprit

  Kiến thức lập trình

I have a problem with python selenium’s chromedriver connection.About half the time I run my tests to connect to a server I am hosting, selenium throwws a connection timed out error like this

testscertificatetest_mysoftware_Netwrk_ClientGroup.py:56: in setup_method
    delete_testClients()
libmysoftware_ClientGroup.py:195: in delete_testClients
    driver.get(website_url)
venvlibsite-packagesseleniumwebdriverremotewebdriver.py:363: in get
    self.execute(Command.GET, {"url": url})
venvlibsite-packagesseleniumwebdriverremotewebdriver.py:354: in execute
    self.error_handler.check_response(response)
venvlibsite-packagesseleniumwebdriverremoteerrorhandler.py:229: in check_response
    raise exception_class(message, screen, stacktrace)
E   selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_CONNECTION_TIMED_OUT
E     (Session info: chrome=128.0.6613.114)
E   Stacktrace:
E       GetHandleVerifier [0x00007FF72A7BB652+29090]
E       (No symbol) [0x00007FF72A72E709]
E       (No symbol) [0x00007FF72A5EB1CA]
E       (No symbol) [0x00007FF72A5E3204]
E       (No symbol) [0x00007FF72A5D4179]
E       (No symbol) [0x00007FF72A5D5F42]
E       (No symbol) [0x00007FF72A5D443F]
E       (No symbol) [0x00007FF72A5D3CD1]
E       (No symbol) [0x00007FF72A5D3C10]
E       (No symbol) [0x00007FF72A5D1AD3]
E       (No symbol) [0x00007FF72A5D214C]
E       (No symbol) [0x00007FF72A5EE231]
E       (No symbol) [0x00007FF72A6873FE]
E       (No symbol) [0x00007FF72A6666EA]
E       (No symbol) [0x00007FF72A6865D9]
E       (No symbol) [0x00007FF72A666493]
E       (No symbol) [0x00007FF72A6309B1]
E       (No symbol) [0x00007FF72A631B11]
E       GetHandleVerifier [0x00007FF72AAD8CDD+3295277]
E       GetHandleVerifier [0x00007FF72AB248C3+3605523]
E       GetHandleVerifier [0x00007FF72AB1A787+3564247]
E       GetHandleVerifier [0x00007FF72A876F36+797318]
E       (No symbol) [0x00007FF72A73988F]
E       (No symbol) [0x00007FF72A735474]
E       (No symbol) [0x00007FF72A735600]
E       (No symbol) [0x00007FF72A724A9F]
E       BaseThreadInitThunk [0x00007FFB1DDD7374+20]
E       RtlUserThreadStart [0x00007FFB1FADCC91+33]

I initially thought this might be from version missmatch so I set both google chrome and chromedriver versions to 128.0.6613.114. I thought that solved the issue but it is persisting.
I am using selenium 4.18.1 with python 3.10 on a windows 10 machine. While half of the time these errors are comming from specific set of tests.

I am using following driver options

def get_driver_options():
    options = webdriver.ChromeOptions()
    options.add_argument("--headless=new")
    options.add_argument("--window-size=1440, 900")
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--allow-running-insecure-content')
    options.add_argument("--disable-extensions")
    options.add_argument("--proxy-server='direct://'")
    options.add_argument("--proxy-bypass-list=*")
    options.add_argument("--start-maximized")
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--FontRenderHinting[none]")
    options.add_argument('--no-sandbox')
    options.add_argument('log-level=3')
    options.add_argument('--ignore-ssl-errors=yes')
    options.add_argument('--allow-insecure-localhost')
    options.add_argument('--remote-allow-origins=*')
    options.add_argument('--disable-search-engine-choice-screen')
    return options

Also I am using a singleton webdriver class to keep my tests from getting too much memory.

class SdriverMeta(type):
    _instances =  {}
    _lock: Lock = Lock()

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            #with cls._lock:

            #if cls not in cls._instances:
            try:

                instance = super().__call__(*args, **kwargs)
                cls._instances[cls] = instance
            except Exception as e:
                # Ensure that the lock and instance are properly cleaned up
                if cls in cls._instances:
                    del cls._instances[cls]
                raise
        return cls._instances[cls]

    @classmethod
    def get_lock(cls):
        return cls._lock

    @classmethod
    def reset_instance(cls):
        # This method can be called to reset the Singleton instance
        with cls._lock:
            if cls in cls._instances:
                del cls._instances[cls]

class SDriver(metaclass=SdriverMeta):
    _instance = None
    def __new__(cls,headless = True):
        if not hasattr(cls, "_instance") or cls._instance is None:
            with cls._lock:

                cls._instance = super(SDriver, cls).__new__(cls)
                cls._instance.headless = headless
                cls._instance.driver = None


        return cls._instance

    def get_driver(self,headless = True):
        #self.close_driver()
        with SdriverMeta.get_lock():


            if self.driver is None:
                if headless:
                    options = get_driver_options()
                else:
                    options = get_driver_headfull_options()
                service = Service()
                self.driver = webdriver.Chrome(service=service, options=options) 
                self.driver.implicitly_wait(90)
        return self.driver


    def close_driver(self):
        with SdriverMeta.get_lock():

            if hasattr(self, "driver") and self.driver:
                try:
                    self.driver.close()
                    self.driver.quit()
                except Exception as e:
                    print(f"Failed to close the driver: {e}")
                finally:
                    self.driver = None
        SdriverMeta.reset_instance()
                    #SDriver._instance = None

     

And the error part happens here as I try to get the url of my server:


def delete_testClients():
        sdriver = SDriver()
        headless=True
        driver = sdriver.get_driver(headless)
        website_url = default_config["website_url"]
        driver.get(website_url)

So as it stands I have no Idea what could be causing my webdriver to not to connect to the server address I have for the machine I have. The url is correct as I am getting it from the hosting service and I am using the same json for all the other tests that are working.

The code snippet I posted works half of the time. Other half of the time the singleton driver sdriver cannot establish a connection to the server that I am testing. I have an inkling that this problem is related with the singleton driver but I am not sure. Any help is appreciated.

Update 1: I don’t think that the problem is with the driver settings. If that was the case 30 other tests that use the same driver options would give simmilar errors.

2

@browsermator Had the right solution. Problem was with these driver options.
options.add_argument(“–proxy-server=’direct://'”) options.add_argument(“–proxy-bypass-list=*”)
Once I removed them my script works fine! Thank you.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT