0

I'm opening Firefox browser through selenium(3.141.0) on python 3.9, and it will always start minimized, even though in code I passed function .maximise_window() after opening the browser. When its started this way, it wont execute code to maximise and it wont execute code to switch tabs, and it wont even do it if im trying to switch tabs manually with my mouse.

If I immediately click on the initiated firefox browser on my taskbar when the program is started, it will function normally, but it will open my browsers home tab, and execute my code in new tab. Thats why you may see in code a part that closes that first tab. When i dont do it and when it starts "faulty", the home tab wont be opened.

Im also using the lenght of tabs as an indicator if the browser initiated faulty, where I tried to put it into loop and make it restart till it opens with 2 tabs, but it just wont unless I manually click it.

The only solution so far I can think of is kinda "hacky"...using pyautogui to scan my taskbar after initializing browser and clicking it fast, but I dont really like the idea.

The code goes through my company data warehouse site and manipulates it to download data.


Update

Other hacky solution I found is starting 2 browsers. First won't work, but second will. Meaning that browser works normally IF there is another Firefox browser open at the time.

Snippets of code:

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class DWH_browser:
    def __init__(self):
        self.browser = webdriver.Firefox()
        self.browser.maximize_window()
        self.browser.get("www.letskeepthecompanysiteasecret.org")
        self.len_windows = len(self.browser.window_handles)
        print(len(self.browser.window_handles))#this next part is used to close the extra tab when browser
                                                #started normally and opens an extra tab
        if len(self.browser.window_handles) == 2:
            self.browser.switch_to.window(self.browser.window_handles[0])
            
            self.browser.close()
            self.browser.switch_to.window(self.browser.window_handles[0])
        
        self.a = ActionChains(self.browser)
        time.sleep(5)

DWH = DWH_browser()#and i initiate it in the code "normally"

#This was the other code I tried using to initate the browser and restart till its in 2 tabs, but not working
# issue_lenght = 1
# while issue_lenght == 1:
#     DWH = DWH_browser()
#     issue_lenght = DWH.len_windows
#     if DWH.len_windows == 1:
#         DWH.browser.quit()
#         print("RESTARTING BROWSER")
        
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Dava
  • 11
  • 4

1 Answers1

0

Summarizing your issues:

  1. Always start minimized.
  2. Passed function maximise_window() after opening the browser.
  3. Won't execute code to switch tabs.

To get rid of all these issues you need to ensure that:

  • Selenium is upgraded to current levels Version 4.4.0.
  • GeckoDriver is updated to current GeckoDriver v0.31.0 level.
  • Firefox Browser is updated to current firefox=103.0.2.

Additionally, you won't be needing self.browser.maximize_window() as by default opens in a maximized mode.

Finally, trying to switch tabs manually with my mouse is a big no as the program execution may get interupted.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352