How to use Selenium with Python? How to use Selenium with Python? python python

How to use Selenium with Python?


You mean Selenium WebDriver? Huh....

Prerequisite: Install Python based on your OS

Install with following command

pip install -U selenium

And use this module in your code

from selenium import webdriver

You can also use many of the following as required

from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementException

Here is an updated answer

I would recommend you to run script without IDE... Here is my approach

  1. USE IDE to find xpath of object / element
  2. And use find_element_by_xpath().click()

An example below shows login page automation

#ScriptName : Login.py#---------------------from selenium import webdriver#Following are optional requiredfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementExceptionbaseurl = "http://www.mywebsite.com/login.php"username = "admin"password = "admin"xpaths = { 'usernameTxtBox' : "//input[@name='username']",           'passwordTxtBox' : "//input[@name='password']",           'submitButton' :   "//input[@name='login']"         }mydriver = webdriver.Firefox()mydriver.get(baseurl)mydriver.maximize_window()#Clear Username TextBox if already allowed "Remember Me" mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()#Write Username in Username TextBoxmydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)#Clear Password TextBox if already allowed "Remember Me" mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()#Write Password in password TextBoxmydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)#Click Login buttonmydriver.find_element_by_xpath(xpaths['submitButton']).click()

There is an another way that you can find xpath of any object -

  1. Install Firebug and Firepath addons in firefox
  2. Open URL in Firefox
  3. Press F12 to open Firepath developer instance
  4. Select Firepath in below browser pane and chose select by "xpath"
  5. Move cursor of the mouse to element on webpage
  6. in the xpath textbox you will get xpath of an object/element.
  7. Copy Paste xpath to the script.

Run script -

python Login.py

You can also use a CSS selector instead of xpath. CSS selectors are slightly faster than xpath in most cases, and are usually preferred over xpath (if there isn't an ID attribute on the elements you're interacting with).

Firepath can also capture the object's locator as a CSS selector if you move your cursor to the object. You'll have to update your code to use the equivalent find by CSS selector method instead -

find_element_by_css_selector(css_selector) 


There are a lot of sources for selenium - here is good one for simple use Selenium, and here is a example snippet too Selenium Examples

You can find a lot of good sources to use selenium, it's not too hard to get it set up and start using it.


You just need to get selenium package imported, that you can do from command prompt using the command

pip install selenium

When you have to use it in any IDE just import this package, no other documentation required to be imported

For Eg :

import selenium print(selenium.__filepath__)

This is just a general command you may use in starting to check the filepath of selenium