Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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 selenium.webdriver.common.by import By

from selenium.webdriver.support.ui 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 : 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 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 -

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

find_element_by_css_selector(css_selector) 

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...