๐Ÿ”ฐ Getting Started with Selenium & Python

๐Ÿ”ฐ Getting Started with Selenium & Python

Selenium is a powerful open-source tool for automating web browsers. Combined with Python, it becomes an excellent choice for tasks like:


Automated web testing


Web scraping


Repetitive browser tasks


✅ What You’ll Learn

What Selenium is


How to install Selenium for Python


How to launch a browser and automate basic tasks


Writing your first Selenium script


๐Ÿงฉ What is Selenium?

Selenium automates browsers using drivers like ChromeDriver, GeckoDriver (Firefox), etc. It simulates human interactions with web elements such as clicking, typing, and submitting forms.


⚙️ Step 1: Install Selenium

You can install Selenium with pip:


bash

Copy

Edit

pip install selenium

You’ll also need a WebDriver (e.g., for Chrome):


Download ChromeDriver (match your Chrome version)


Place the driver in a known path or the same directory as your script.


๐Ÿ–ฅ️ Step 2: Your First Selenium Script

python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.by import By

import time


# Set up the driver (Chrome in this case)

driver = webdriver.Chrome()  # Make sure chromedriver is in PATH


# Open a website

driver.get("https://www.google.com")


# Wait for a few seconds

time.sleep(2)


# Find the search box and type a query

search_box = driver.find_element(By.NAME, "q")

search_box.send_keys("Selenium with Python")


# Submit the search

search_box.submit()


# Wait and close the browser

time.sleep(5)

driver.quit()

๐Ÿ”Ž Common WebDriver Methods

Task Code Example

Open a URL driver.get("https://example.com")

Find element by name driver.find_element(By.NAME, "q")

Click a button element.click()

Enter text element.send_keys("text")

Submit a form element.submit()

Get page title driver.title

Close browser driver.quit()


๐Ÿ“Œ Tips

Use time.sleep() for simple delays, but prefer WebDriverWait for reliable waits.


Always check for the right locator (By.ID, By.CLASS_NAME, By.XPATH, etc.)


Use browser headless mode if you don't need a GUI.


python

Copy

Edit

from selenium.webdriver.chrome.options import Options


options = Options()

options.add_argument("--headless")

driver = webdriver.Chrome(options=options)

๐Ÿ” Best Practice: Use WebDriverWait

python

Copy

Edit

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 10)

element = wait.until(EC.presence_of_element_located((By.NAME, "q")))

๐Ÿง  Summary

Component Purpose

webdriver Automates the browser

find_element Locates elements for interaction

send_keys() Simulates typing

click() / submit() Interacts with buttons/forms

WebDriverWait Waits dynamically for elements to load

Learn Selenium Python Training in Hyderabad

Read More

How to Integrate Selenium with Python Logging for Better Test Reports

Automating Social Media Login and Posting Using Selenium

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions


Comments

Popular posts from this blog

Handling Frames and Iframes Using Playwright

Cybersecurity Internship Opportunities in Hyderabad for Freshers

Tosca for API Testing: A Step-by-Step Tutorial