Skip to content
Advertisement

monitor chrome tabs – python selenium

I’m writing a script where I need you to monitor the number of open tabs. The script reads a table and each information in the table opens a new tab in the browser. It turns out that in order not to have thousands of tabs open, I need the script to monitor the tabs to have only 3 open, when I close a tab I need to automatically open the next tab

def base_dados_processos(self):
    try:
        df = pd.read_excel('TABLE.xlsx')
        self.num_proc = df['COLUM']
    except Exception:
        pg.alert(text='error', title='ERROR', button='OK')
        self.chrome.quit()


def loop_pesquisa(self):
    for PROCESSOS in zip(self.num_proc):
        num_current_tabs = len(self.chrome.window_handles)
        if num_current_tabs < 3:
            pg.hotkey('ctrl', '1')
            time.sleep(1)
            self.chrome.get('https://stackoverflow.com/')
            pg.write(str(test))
            pg.press('enter')
            time.sleep(3)
            pg.press('tab', presses=26)
            pg.press('enter')
            time.sleep(1)
            pg.press('enter')

Advertisement

Answer

To do this, just add a loop that monitors the guides, it can be as follows:

while len(self.chrome.window_handles) > 3:
            time.sleep(0.5)

without your code:

def base_dados_processos(self):
    try:
        df = pd.read_excel('TABLE.xlsx')
        self.num_proc = df['COLUM']
    except Exception:
        pg.alert(text='error', title='ERROR', button='OK')
        self.chrome.quit()


def loop_pesquisa(self):
    for PROCESSOS in zip(self.num_proc):
        while len(self.chrome.window_handles) > 3:
            time.sleep(0.5)
        pg.hotkey('ctrl', '1')
        time.sleep(1)
        self.chrome.get('https://stackoverflow.com/')
        pg.write(str(test))
        pg.press('enter')
        time.sleep(3)
        pg.press('tab', presses=26)
        pg.press('enter')
        time.sleep(1)
        pg.press('enter')
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement