Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

How do I set up Selenium to work with Python? I just want to write/export scripts in Python, and then run them. Are there any resources that will teach me how to do this? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python.

1 Answer

0 votes
by (62.9k points)

I am assuming you meant, Selenium WebDriver.

 Prerequisite: Install Python based on your OS

 Install with the following command

pip install -U selenium

And use this module in your code

from selenium import webdriver

You can import any of the following libraries as required

from import By

from import Select

from selenium.common.exceptions import NoSuchElementException

Below is an updated answer

I would like to recommend you to run the script without IDE. Here is an approach you can follow.

 Try using IDE to find xpath of an element

And use find_element_by_xpath().click()

An example below shows login page automation

#ScriptName :


from selenium import webdriver


#Following are optional required

from import By

from import Select

from selenium.common.exceptions import NoSuchElementException


baseurl = ""

username = "admin"

password = "admin"


xpaths = { 'usernameTxtBox' : "//input[@name='username']",

           'passwordTxtBox' : "//input[@name='password']",

           'submitButton' :   "//input[@name='login']"



mydriver = webdriver.Firefox()




#Clear Username TextBox if already allowed "Remember Me" 



#Write Username in Username TextBox



#Clear Password TextBox if already allowed "Remember Me" 



#Write Password in password TextBox



#Click Login button


There is 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 the below browser pane, find and select an element by "xpath" through the Chropath.
  5. Mouse hover onto the element on the webpage in the xpath textbox, you will get xpath of an object/element.
  6. Copy Paste xpath to the script.
  7. Run script -


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 mouse hover over the element. You'll have to update your code to use the equivalent find by CSS selector method instead -


Browse Categories