First Selenium Script in Python: Open Google and Search

First Selenium Script in Python: Open Google and Search

✅ Prerequisites

Make sure you have the following installed:


Python (≥ 3.6)


selenium library


Chrome browser


ChromeDriver (match version with your Chrome)


You can install Selenium using:


bash

Copy

Edit

pip install selenium

πŸ§ͺ Script: Open Google and Search

python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import time


# Set up the driver (Make sure chromedriver is in your PATH or provide executable_path)

driver = webdriver.Chrome()


# Open Google

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


# Wait a little for the page to load

time.sleep(2)


# Find the search box

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


# Type the search query

search_box.send_keys("Selenium with Python")


# Press Enter

search_box.send_keys(Keys.RETURN)


# Wait a few seconds to see results

time.sleep(3)


# Optional: Close the browser

driver.quit()

πŸ“ Notes:

If you face issues with ChromeDriver, make sure it matches your installed Chrome version.


For better stability and performance, consider using WebDriverWait instead of time.sleep() for real projects.


Would you like me to show how to do this using Firefox or Edge instead of Chrome?

Learn Selenium Python Course in Hyderabad

Read More

Introduction to Selenium with Python: What & Why?

How do I click buttons in Selenium (Python)?

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know