Skip to content
CodeKruPro

Automation tool (Selenium) · Python

Selenium basics and browser control

WebDriver architecture, starting a browser, navigation and closing it.

Learn: Selenium basics in Python: how WebDriver works and controls the browser7 minute read

Selenium is a free, open-source tool (its code is public and anyone can use it) that controls a real web browser from code. Your Python script says "open this page, type here, click there", and Selenium makes the browser do it. The first questions in an automation interview check three things: that you know what Selenium is and what it is not, how your Python code talks to the browser, and how you start and stop a browser without leaving it running. This page covers Selenium 4 in Python.

What Selenium is made of

WebDriver is the API you call from code, installed with pip install selenium. Selenium Grid runs browsers on other machines, so tests can run in parallel. Selenium IDE is a browser extension that records and plays back steps. Selenium Manager, shipped since version 4.6, finds or downloads a matching browser driver when you have not set one up.

Selenium is not a test runner. It has no test discovery (finding your tests automatically), no fixtures (setup and clean-up functions that run around a test) and no reports. Pair it with pytest, a test library that does these jobs.

How a command reaches the browser

  1. The selenium package (the client library, also called the language binding) turns your call, for example driver.find_element(By.ID, "user"), into an HTTP request with a JSON body. HTTP is the way programs send requests over a network, and JSON is a plain-text format for data.
  2. The request follows the W3C WebDriver protocol, the web standard for controlling a browser. All major browsers support it. Selenium 4 sends its normal commands (find an element, click, type, read text) this way. A few extra features, such as DevTools access (Chrome DevTools Protocol) and event listening (WebDriver BiDi), use a separate connection that stays open next to it.
  3. A browser driver, a small local server program such as chromedriver or geckodriver, receives the request.
  4. The driver controls the browser and returns a JSON response, for example an id for the element it found.
The test calls the client library, which sends an HTTP request to the browser driver, which drives the browser; responses come back the same way, optionally through Selenium Grid.
Every Selenium command is one request and one response.

Each call is a round trip, so fewer and better lookups make tests faster and steadier. In the code, webdriver.Chrome is a class built on the remote WebDriver class in selenium.webdriver.remote.webdriver. webdriver.Remote uses the same base class but sends the requests to Selenium Grid, so tests do not change.

A first script, step by step

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")

driver = webdriver.Chrome(options=options)
try:
    driver.get("https://example.com")
    print(driver.title)
finally:
    driver.quit()
  1. Build the browser settings with an options object. Headless means no visible window, which is normal on build servers (machines that run your tests automatically, with no screen).
  2. Set a window size, because the default headless window is small and pages may hide menus.
  3. Start Chrome. Selenium Manager finds the driver if needed.
  4. get() opens the page and waits until it has loaded.
  5. quit() in finally runs even when a step fails, so no browser is left open. A finally block always runs after the try block, with or without an error.

The driver also works as a context manager, an object that cleans up when a with block ends:

with webdriver.Chrome(options=options) as driver:
    driver.get("https://example.com")
    print(driver.title)

In a pytest framework, a fixture does the same job. The fixture creates the driver, then yield hands it to the test and pauses the fixture. When the test ends, the fixture continues and calls quit().

Navigation and page details

Python has no navigate() object; the methods sit on the driver. driver.get(url) opens a page, and driver.back(), driver.forward() and driver.refresh() move through history. driver.title, driver.current_url and driver.page_source are properties, so you write them without brackets. driver.set_window_size(1024, 768) sets the window.

Options objects also carry settings such as page_load_strategy, accept_insecure_certs and Chrome preferences through add_experimental_option("prefs", {...}). The old desired_capabilities argument is gone and raises TypeError.

close() versus quit()

close() closes only the current window or tab. The session goes on, and you must switch to another open window before the next command, or you get NoSuchWindowException. quit() closes everything and ends the session.

How to answer in an interview

For "What is Selenium?", give a one-sentence definition, name the parts, then one strength and one limit. For "Explain the architecture", walk through binding, protocol, driver and browser, and finish with "each command is one HTTP round trip". For "What changed in Selenium 4?", mention find_element(By.X, ...) lookups, the W3C WebDriver protocol (it replaced the old JSON Wire protocol), locate_with relative locators, switch_to.new_window() and Selenium Manager.

Common mistakes

  • Writing driver.title(). title is a string property, so calling it raises TypeError.
  • Copying find_element_by_id from an old tutorial. Those helpers were removed in 4.3 and raise AttributeError.
  • Using close() in teardown (the clean-up step after each test) instead of quit().
  • Forgetting the limits: no operating system dialogs (windows opened by Windows or macOS, not by the web page), no CAPTCHAs (the "prove you are human" puzzles), no native mobile apps (apps installed on a phone; Appium covers those), and no automatic waiting for page changes.

Recap

Selenium drives real browsers, sending normal commands through the W3C WebDriver protocol: your code, then the binding, then the driver, then the browser. Configure the browser with an options object, read page details through properties, and make sure quit() always runs through finally, a with block or a fixture.

14 questions0/14 solved

StatusQuestionDifficultyAsked in
Not startedOpen a browser, open a page and print its titleEasyAsked in: Accenture, Capgemini, Infosys +3
Not startedWhat is Selenium? Parts, strengths and limitsEasyAsked in: Accenture, Infosys, TCS +1
Not startedVisit two pages one after anotherEasyAsked in: Accenture, Infosys, TCS +1
Not startedGo back and forward in the browser historyEasyAsked in: Accenture, Infosys, TCS +1
Pro+What is webdriver.Chrome in Python?EasyAsked in: Accenture, Infosys, Wipro
Pro+close() versus quit()EasyAsked in: Accenture, TCS, Wipro
Pro+Chrome is installed but there is no chromedriver. What happens?Easy-MediumAsked in: Accenture, Infosys, Wipro
Pro+Selenium WebDriver architecture in PythonMediumAsked in: Accenture, Infosys, Wipro
Pro+What changed in Selenium 4 (Python)?MediumAsked in: Accenture, Infosys, Wipro
Pro+Navigation methods in Selenium PythonEasyAsked in: Accenture, Infosys, TCS +1
Pro+Start Chrome headless with a fixed window size (Python)Easy-MediumAsked in: Capgemini, Cognizant, Infosys +1
Not startedSet the browser window size and read it backEasy
Not startedQuit the browser with a with statementEasy-MediumAsked in: Accenture, TCS, Wipro
Not startedRead the page source and search itEasy
Not startedNot startedIn progressIn progressSolvedSolvedPro+Pro+

The first 2 questions of each topic are free. Pro+ opens every question.

Back to the plan