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
Comments
Post a Comment