2

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?

7
  • Does this answer your question? Selenium: How to stop the browser window getting on-top for every get?
    – Beatso
    Oct 12, 2022 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. Oct 12, 2022 at 20:37
  • @BarrythePlatipus, would you please explain your answer? I don't see anything here.
    – ar2015
    Oct 12, 2022 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? Oct 12, 2022 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, 2022 at 20:52

2 Answers 2

2

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).

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.