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 for that? I tried googling, but the stuff I found was either referring to an outdated version of Selenium (RC), or an outdated version of Python.
3 Answers
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 By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
Here is an updated answer
I would recommend you to run script without IDE... Here is my approach
- USE IDE to find xpath of object / element
- And use find_element_by_xpath().click()
An example below shows login page automation
#ScriptName : Login.py
#---------------------
from selenium import webdriver
#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "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 TextBox
mydriver.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 TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)
#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()
There is an another way that you can find xpath of any object -
- Install Firebug and Firepath addons in firefox
- Open URL in Firefox
- Press F12 to open Firepath developer instance
- Select Firepath in below browser pane and chose select by "xpath"
- Move cursor of the mouse to element on webpage
- in the xpath textbox you will get xpath of an object/element.
- 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)

- 259
- 3
- 12

- 3,693
- 8
- 35
- 42
-
Thanks I got pip to work, and installed selenium that way. I exported a test case from the IDE as a .py file, and when I try to reopen it, I get a `Error loading test case: no command found` error. – Carpetfizz Jul 09 '13 at 14:37
-
>>>> What do you mean by reopen file? >>>> Do you mean execute python script which is recorded by IDE? >>> What are you trying to achieve? – Abhishek Kulkarni Jul 10 '13 at 07:04
-
Basically this: `1.) Record simple script in IDE, export to Python`, `2.) Edit the script with Python code` `3.) Run the script through the IDE` Also, is there any other way to run the Python script? – Carpetfizz Jul 11 '13 at 05:42
-
@AbhishekKulkarni- I have one question- should I use an IDE like IDLE like we have Indigo? Or some other means? – demouser123 Nov 17 '14 at 05:51
-
@geeko_zac - Doesn't matter. Whichever IDE you find easy to use for. Code quality is important :) – Abhishek Kulkarni Nov 19 '14 at 04:45
-
@Abhishek Kulkarni hi, thanks for a good explanation. Could you give an advice how to wrap that case in class of function in order to use authentification in other tests? – Leo Jul 21 '16 at 15:34
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.

- 7,925
- 13
- 52
- 71
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

- 119
- 4