1

I have 40 Python unit tests and each of them open a Selenium driver as they are separate files and cannot share the same driver.

from selenium import webdriver
webdriver.Firefox()

The above commands will take the focus to the new opened window. For example, if I am on my editor and typing something, in the middle of my work, suddenly a selenium browser is opening and Linux switch to that window. I am not sure if Windows or Mac have a similar problem or not.

This means that every time I run a unit, I cannot use my computer as it keeps switching away from the application that I am currently using.

How can I tell Selenium not to switch to the opened window?

ar2015
  • 5,558
  • 8
  • 53
  • 110
  • Does this answer your question? [Selenium: How to stop the browser window getting on-top for every get?](https://stackoverflow.com/questions/16741089/selenium-how-to-stop-the-browser-window-getting-on-top-for-every-get) – Beatso Oct 12 '22 at 20:34
  • 1
    Afaik there isn't a `start_minimized()` method for Selenium. You could run it headless tho, and that won't steal focus. – Barry the Platipus Oct 12 '22 at 20:37
  • @BarrythePlatipus, would you please explain your answer? I don't see anything [here](https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/firefox/webdriver.py). – ar2015 Oct 12 '22 at 20:49
  • What do you mean, @ar2015 - are you unfamiliar with headless mode in Firefox/python? You want me to pen down a quick example? – Barry the Platipus Oct 12 '22 at 20:51
  • @BarrythePlatipus, yes please. I ran `driver.minimize_window()` no success. `driver.start_minimized()` also does not exist. If can minimize, that will be even better so if it fails, I can see where the problem shows up. – ar2015 Oct 12 '22 at 20:52
  • This is what I wrote above, that such method does **not** exist in Selenium (as opposed to `maximize_window()`, which exists). However, you can run the browser in headless mode. Would you like an example? – Barry the Platipus Oct 12 '22 at 20:54
  • @BarrythePlatipus, yes please. – ar2015 Oct 12 '22 at 20:56

2 Answers2

1

Here is an example of running Selenium/Firefox on linux, in headless mode. You can see various imports as well - gonna leave them there. Browser will start in headless mode, will go to ddg page and print out the page source, then quit.

from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC

firefox_options = Firefox_Options()

firefox_options.add_argument("--width=1280")
firefox_options.add_argument("--height=720")
firefox_options.headless = True

driverService = Service('chromedriver/geckodriver') ## path where geckodriver is

browser = webdriver.Firefox(service=driverService, options=firefox_options)
wait = WebDriverWait(browser, 20)

browser.get('https://duckduckgo.com')
print(browser.page_source)
browser.quit()

Browser can also take screenshots while in headless mode, if any unit test requires it.

A reasonably well structured documentation for Selenium can be found at https://www.selenium.dev/documentation/ (with some gaps, of course, but generally decent).

Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
1

You can run your Selenium drivers in headless mode. This will prevent moving focus on opened by Selenium browsers and will not disturb you working on your PC machine.

Prophet
  • 32,350
  • 22
  • 54
  • 79