Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Selenium by (9.5k points)

How to set up Selenium to write/export scripts in Python? What are the resources for that? 

1 Answer

0 votes
by (19.7k points)
edited by

For the Selenium WebDriver installation, as a prerequisite 

  1. Install Python based on your OS

  2. Use this command for the installation\

pip install -U selenium

  1. Add  this module to your code

from selenium import webdriver

  1. The following commands can be used as well

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

from selenium.common.exceptions import NoSuchElementException

Here’s the alternative to do it: 

Run the script without IDE. Below is the approach I’ve used 

USE IDE to find xpath of object / element

And use find_element_by_xpath().click()

 Example: For 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()

You can also use this way to 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. xpath text box ----> xpath of an object/element.

  7. Copy Paste xpath to the script.

Do run this script 

python Login.py 

Try to use a CSS selector instead of xpath. Because CSS selectors are quite faster than xpath. It’s usually preferred over xpath when you don’t have an ID attribute on the elements. 

You can  capture the object's locator as a CSS selector using Firepath if you move the cursor to the object. To achieve this you need to update your code. You can find it by the CSS selector method. 

find_element_by_css_selector(css_selector)

If you want to learn more about Selenium? Check out our course here: Selenium course on Intellipaat 

Learn Selenium automation testing with our comprehensive Selenium Tutorial and ace your next job interview with our Selenium Interview Questions!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 6, 2020 in Selenium by Justin (7k points)
0 votes
1 answer
0 votes
1 answer
asked Aug 19, 2020 in Selenium by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Aug 19, 2020 in Selenium by Sudhir_1997 (55.6k points)

Browse Categories

...