Notify you when you’ve opened too many tabs…?

I’ve been trying to make a code using selenium, tkinter to monitor/track your tab and notify you when you have tooo many tabs opened.

I want the program to notify me when I have more than 4 tabs open. I use the code to open 4 tabs in default and want to see if it notify message me when I opened the 5th tab maually…but it seems to not be working. Any advice? :((((

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from tkinter import messagebox
import time


while True:
  #Use Firefox and go to google
  driver = webdriver.Firefox()
  driver.get("http://www.google.com/")

  #Open tab
  driver.execute_script("window.open('');")

  #Switch focus to new tab
  driver.switch_to.window(driver.window_handles[1])

  driver.get("http://www.stackoverflow.com/")

  #Open tab
  driver.execute_script("window.open('');")
  #Open tab
  driver.execute_script("window.open('');")

  #print(type(driver.window_handles))
  print(len(driver.window_handles))
  print("n")

  #Counting tabs
  tabs_num= len(driver.window_handles)

  #pop up notify if more than 5 tab
  if tabs_num > 4:
    messagebox.showinfo('yeet', 'fleek')
    break

  time.sleep(10)


Answer

When you try to display a message box using tkinter you will also get a unreliable main window also that will have no use, Also you cannot only display the messagebox. So I would suggest you to make a message box out of the main window. The code is given below.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from tkinter import messagebox
import time
from tkinter import *
root = Tk()
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get("http://www.google.com/")

driver.execute_script("window.open('');")

driver.switch_to.window(driver.window_handles[1])
driver.get("http://www.stackoverflow.com/")
driver.execute_script("window.open('');")
driver.execute_script("window.open('');")

while True:
    tabs_num= len(driver.window_handles)
    if tabs_num > 4:
        
        my_button = Button(root,
                   text = "ok",
                   command = root.destroy)
        
        my_label = Label(root,text = "yeet")
        my_label.pack()
        root.lift()
        my_button.pack()
        break