Can you provide a beginner-friendly tutorial for using Selenium with Python?

Can you provide a beginner-friendly tutorial for using Selenium with Python?

My concern is : 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.

Setting up Selenium with Python is straightforward, and you can use the latest versions of Selenium and Python.

Here’s a step-by-step guide to getting started:

Install Python:

Download and install Python from the official website: Python Downloads. (Download Python | Python.org). During installation, make sure to check the box that says “Add Python to PATH.”

Install Selenium:

Open a command prompt (Windows) or terminal (macOS/Linux). Install the Selenium package using pip, Python’s package installer. Run the following command:

pip install selenium

Download a WebDriver:

Selenium requires a WebDriver to interface with different browsers. You can download the WebDriver for your preferred browser:

  • Chrome: ChromeDriver
  • Firefox: GeckoDriver
  • Edge: Microsoft Edge Driver
  • Safari: SafariDriver is included in Safari’s Developer Tools. Enable it from Safari > Preferences > Advanced > Show Develop menu in menu bar.

Set Up the WebDriver:

After downloading, extract the WebDriver executable (e.g., chromedriver.exe for Chrome) and place it in a location accessible from your PATH environment variable. Alternatively, you can specify the path to the WebDriver executable in your script.

Write Your First Selenium Script:

Here’s a simple example to open a browser, navigate to a webpage, and get its title using Python and Selenium:

from selenium import webdriver

# Path to the WebDriver executable
driver = webdriver.Chrome(executable_path='path_to_chromedriver.exe')

# Open a website
driver.get('https://www.example.com')

# Get the title of the page
print(driver.title)

# Close the browser
driver.quit()

Run Your Script:

Save the above script to a file with a .py extension (e.g., selenium_script.py). Open a command prompt or terminal. Navigate to the directory where your script is saved. Run the script using Python:

python selenium_script.py

This setup should work with the latest versions of Selenium and Python. If you encounter any issues, make sure to check the Selenium and WebDriver documentation for any updates or changes.

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)