I am automating the metamask extension with selenium 4.17.2 and python 3.11.4. Metmask is an extension which pops up when it is called from a website. After it does, you have to press a number of buttons so you confirm everything. After pressing all of the confirm buttons, the popup closes on its own. Below is the function that does this: it finds the metamask window and repeats the pressing of buttons for repeat_count and while the window is opened (the window can close/open multiple times thus the repeat_count). The problem is that, sometimes, when the popup closes after confirming, the driver seems to lose connection (as if it used driver.quit() on its own), even though the NoSuchWindowException should be raised instead.

def confirm_all(self, driver, repeat_count=2, timeout=5):
    wait = WebDriverWait(driver, 3)
    current_handle = driver.current_window_handle
    

        try:
            metamask_handle = self.find_window_by_name(driver, 'MetaMask Notification', current_handle)[0]
        except Exception as exc:
            print('Exception while trying to find metamask window in confirm_all:', type(exc).__name__)
            log(self.logger, 'exception', 'metamask window exception')
            return
        driver.switch_to.window(metamask_handle)
        while metamask_handle in driver.window_handles:
            try:  
                self.scroll_down_metamask(driver)
                try:
                    print('try to find blue button one')
                    if not driver.service.is_connectable():
                        log(self.logger, 'warning', 'driver is not connectable b1')
                        print('driver is not connectable b1')
                    else:
                        log(self.logger, 'info', 'driver is connectable b1')
                        print('driver is connectable b1')
                    blue_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[(@data-testid="page-container-footer-next")]')))
                except:
                    print('try to find blue button two')
                    if not driver.service.is_connectable():
                        log(self.logger, 'warning', 'driver is not connectable b2')
                        print('driver is not connectable b2')
                    else:
                        log(self.logger, 'info', 'driver is connectable b2')
                        print('driver is connectable b2')
                    blue_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[(@data-testid="confirmation-submit-button")]')))


                blue_button.click()
                print('button clicked')
                
                time.sleep(timeout)

            except TimeoutException:
                print('metamask confirmation error')
                log(self.logger, 'exception', 'Timeout exception in metamask confirm_all')
                break
            except (NoSuchWindowException, StaleElementReferenceException, NoSuchAttributeException) as exc:
                exc_type, exc_info, exc_traceback = sys.exc_info()
                print('excepted metamask:', exc_type.__name__)
                log(self.logger, 'exception', 'Other exception in metamask confirm_all')
                break
            except NotEnoughFundsException as exc:
                log(self.logger, 'exception', 'Metamask not enough funds')
                driver.close()
                driver.switch_to.window(current_handle)
                raise exc
            except Exception as exc:
                print('exception occured while in metamask:', type(exc).__name__)
                log(self.logger, 'exception', 'Random exception in metamask confirm_all')
                break
        
        

    try:
        driver.close()
    except NoSuchWindowException:
        print('metamask window closed on its own')

    driver.switch_to.window(current_handle)

The first iteration is always succesful (which indicates that the connection was good when we started operating on metamask), but if the extension closes after confirming (again, not always, maybe about a 1/5 chance), the MaxRetryError occurs and messes with other driver operations that come after this function.

The error usually happens when trying to find one of the blue_button, because sometimes the print(`driver is not connectable b1/b2') can get called in either place.

MaxRetryError Selenium MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))

These references can give insight to why the error occurs but I can't match their solutions with my issue, as I do not quit the driver during this function.

As you can see by the time.sleep(timeout), I have tried to add a delay, and it seemed like it lessened the chance of the error, but didn't solve the issue.

The full traceback is similar to those in the references above, except that it occurs usually when waiting for the buttons.

Would be very thankful for any solutions/ideas for this issue :).

0

There are 0 best solutions below