Selenium Python

Posted : admin On 1/26/2022

I'm beginning the automate the boring stuff book and I'm trying to open a chrome web browser through python. I have already installed selenium and I have tried to run this file: from selenium imp.

  1. Selenium Python Framework Git
  2. Selenium Python Tutorial
  3. Installing Selenium For Python
  4. Selenium Python Bot
  5. Selenium Python Bdd
  6. Selenium Python Download
  7. Selenium Python Wait For Page To Load
  1. Selenium - Web Automation with Python Selenium is a web automation framework. It starts a web browser and any task that can be done typically on the web, Selenium+Python can do for you.
  2. Explicit Waits¶. An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep, which sets the condition to an exact time period to wait.

Python selenium commands cheat sheet

Frequently used python selenium commands – Cheat Sheet

To import webdriver module in python use below import statement

Driver setup:
firefoxdriver = webdriver.Firefox(executable_path=”Path to Firefox driver”)

To download: Visit GitHub

chromedriver = webdriver.Chrome(executable_path=”Path to Chrome driver”)

To download: Visit Here

Internet Explorer:
iedriver = webdriver.IE(executable_path=”­Pat­h To­ IEDriverServer.exe”)

To download: Visit Here

edgedriver = webdriver.Edge(executable_path=”­Pat­h To­ MicrosoftWebDriver.exe”)

To download: Visit Here

operadriver = webdriver.Opera(executable_path=”­Pat­h To­ operadriver”)

To download: visit GitHub


SafariDriver now requires manual installation of the extension prior to automation

Browser Arguments:


To open browser in headless mode. Works in both Chrome and Firefox browser


To start browser maximized to screen. Requires only for Chrome browser. Firefox by default starts maximized


To open private chrome browser


To disable notifications, works Only in Chrome browser



To Auto Download in Chrome:

To Auto Download in Firefox:

Selenium Python

We can add any MIME types in the list. MIME for few types of files are given below.

  1. Text File (.txt) – text/plain
  2. PDF File (.pdf) – application/pdf
  3. CSV File (.csv) – text/csv or “application/csv”
  4. MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet or application/
  5. MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document
    Zip file (.zip) – application/zip

The value of can be set to either 0, 1, or 2.

0 – Files will be downloaded on the user’s desktop.
1 – Files will be downloaded in the Downloads folder.
2 – Files will be stored on the location specified for the most recent download

Disable notifications in Firefox

firefoxOptions.set_preference(“dom.webnotifications.serviceworker.enabled”, false);
firefoxOptions.set_preference(“dom.webnotifications.enabled”, false);

Open specific Firefox browser using Binary:

Open specific Chrome browser using Binary:
from selenium import webdriver
from import Options

options = Options()
options.binary_location = “”
driver = webdriver.Chrome(chrome_options=options, executable_path=””)

Read Browser Details:


Go to a specified URL:


Locating Elements:

driver.find_element_by_ – To find the first element matching the given locator argument. Returns a WebElement

driver.find_elements_by_ – To find all elements matching the given locator argument. Returns a list of WebElement


<input id=”q” type=”text” />

element = driver.find_element_by_id(“q”)

By Name

<input id=”q” name=”search” type=”text” />

element = driver.find_element_by_name(“search”)

By Class Name

<div class=”username” style=”display: block;”>…</div>

Selenium Python Framework Git

element = driver.find_element_by_class_name(“username”)

By Tag Name

<div class=”username” style=”display: block;”>…</div>

element = driver.find_element_by_tag_name(“div”)

By Link Text

<a href=”#”>Refresh</a>

element = driver.find_element_by_link_text(“Refresh”)

By Partial Link Text

<a href=”#”>Refresh Here</a>

element = driver.find_element_by_partial_link_text(“Refresh”)

By XPath

<form id=”testform” action=”submit” method=”get”>

Username: <input type=”text” />
Password: <input type=”password” />


element = driver.find_element_by_xpath(“//form[@id=’testform’]/input[1]”)

By CSS Selector

Selenium Python Tutorial

<form id=”testform” action=”submit” method=”get”>

<input class=”username” type=”text” />
<input class=”password” type=”password” />

Installing Selenium For Python


element = driver.find_element_by_css_selector(“form#testform>input.username”)

Important Modules to Import:

from selenium import webdriver
from import WebDriverWait
from import expected_conditions
from import Select

from import By
from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

from import Options
from selenium.webdriver.firefox.options import Options

Selenium Python

Python Selenium commands for operation on elements:



Selenium Python Bot

Text field:





Find out the select element using any element locating strategies and then select options from list using index, visible text or option value.

Element properties:


These methods return either true or false.

Read Attribute:


Get attribute from a disabled text box



Note: An important note to store screenshots is that save_screenshot(‘filename’) and get_screenshot_as_file(‘filename’) will work only when extension of file is ‘.png’. Otherwise content of the screenshot can’t be viewed

Selenium Python Bdd

Read articles for more details about taking screenshot and element screenshot

Selenium Python Download

The list here contains mostly used python selenium commands but not exhaustive. Please feel free to add in comments if you feel something is missing and should be here.

Selenium Python Wait For Page To Load

3 Responses

  1. […] Previous: Previous post: Execute Python Selenium tests in Selenium GridNext: Next post: Python selenium commands cheat sheet […]

  2. Thank you very much

  3. Hi Sir,

    I am trying to do the sorting in selenium with python using For loop could u please help me is there any way that i can do it ?

    Sorting in descending order in the below website