Interview Questions, Answers and Tutorials

Selenium Scripting Using Python

Selenium Scripting Using Python

Selenium is like a robot that can control a web browser. It can open a website, click on buttons, type in search boxes, and even take screenshots. Imagine having a little helper who can do these tasks for you automatically—Selenium is that helper!

Let’s explore how to use Selenium with Python, step by step.


1. Setting Up Selenium

Step 1: Install Selenium

Open your terminal or command prompt and type this:

pip install selenium

Step 2: Download a WebDriver

A WebDriver is like a driver for your browser. For example:

  • Chrome: Download ChromeDriver from here.
  • Firefox: Download GeckoDriver from here.
  • Edge: Download GeckoDriver from here.

Make sure the WebDriver matches your browser version!


2. A Simple Selenium Script

Let’s write a Python script to open Google and search for something.

Code Example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Step 1: Set up the WebDriver
driver = webdriver.Chrome()  # Or webdriver.Firefox() if using Firefox

try:
    # Step 2: Open Google
    driver.get("https://www.google.com")

    # Step 3: Find the search box
    search_box = driver.find_element(By.NAME, "q")

    # Step 4: Type in the search box
    search_box.send_keys("Selenium Python")
    search_box.send_keys(Keys.RETURN)

    # Step 5: Wait to see the results
    time.sleep(5)  # Let’s wait for 5 seconds

finally:
    # Step 6: Close the browser
    driver.quit()




Explanation
  • driver.get(url): Opens the given URL.
  • find_element: Finds an element on the page (e.g., the search box).
  • send_keys: Types into the element (e.g., types “Selenium Python”).
  • Keys.RETURN: Simulates pressing the “Enter” key.

3. Interacting with Elements

Now, let’s click buttons and fill out forms. We’ll automate logging into a website.

Example: Automating Login

Imagine we’re logging into a fake website.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Step 1: Set up the WebDriver
driver = webdriver.Chrome()

try:
    # Step 2: Open the login page
    driver.get("https://example.com/login")

    # Step 3: Fill in the username and password
    driver.find_element(By.ID, "username").send_keys("myusername")
    driver.find_element(By.ID, "password").send_keys("mypassword")

    # Step 4: Click the login button
    driver.find_element(By.ID, "loginButton").click()

    # Step 5: Wait and check
    time.sleep(5)

finally:
    # Step 6: Close the browser
    driver.quit()




Explanation
  • By.ID: Finds elements by their id attribute.
  • click(): Clicks a button or link.

4. Taking Screenshots

Sometimes, we want to capture the screen as proof.

Example: Taking a Screenshot

from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get("https://example.com")
    driver.save_screenshot("screenshot.png")
finally:
    driver.quit()




Explanation
  • save_screenshot(filename): Saves a screenshot as a file.

5. Waiting for Elements

Web pages don’t always load instantly. We can tell Selenium to wait.

Example: Waiting for an Element

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()

try:
    driver.get("https://example.com")

    # Wait until the element with ID "welcome" is visible
    welcome_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "welcome"))
    )
    print("Found the element:", welcome_element.text)

finally:
    driver.quit()




Explanation
  • WebDriverWait: Waits for a condition (e.g., an element to appear).
  • expected_conditions: Defines conditions like visibility.

Practice Questions

Q1: Open YouTube and Search

Write a script to:

  1. Open YouTube.
  2. Search for “funny cat videos.”
  3. Wait for 5 seconds, then close the browser.

Solution

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()

try:
    driver.get("https://www.youtube.com")
    search_box = driver.find_element(By.NAME, "search_query")
    search_box.send_keys("funny cat videos")
    search_box.send_keys(Keys.RETURN)
    time.sleep(5)
finally:
    driver.quit()





Q2: Log into a Fake Site

Write a script to:

  1. Open a fake login page.
  2. Enter “user123” as the username and “pass123” as the password.
  3. Click the login button.

Solution

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

try:
    driver.get("https://example.com/login")
    driver.find_element(By.ID, "username").send_keys("user123")
    driver.find_element(By.ID, "password").send_keys("pass123")
    driver.find_element(By.ID, "loginButton").click()
finally:
    driver.quit()





Q3: Take a Screenshot

Write a script to:

  1. Open “https://testinganswers.com“.
  2. Save a screenshot as “example.png”.

Solution

from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get("https://testinganswers.com")
    driver.save_screenshot("example.png")
finally:
    driver.quit()





With Selenium, you can automate boring tasks and have fun controlling your browser like a pro. Keep practicing, and soon you’ll be writing complex scripts! 🚀