A Basic script for Webdriver Chrome Python Automation

Here’s a Python script that uses Selenium WebDriver to automate the Chrome browser. Make sure you have both Selenium and ChromeDriver installed, and you’ve set up your Python environment accordingly. Here is a how-to guide: Selenium ChromeDriver download and install.

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

# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Navigate to a website

# Find the search box element
search_box = driver.find_element_by_name("q")

# Type a search query
search_box.send_keys("Selenium WebDriver")

# Press Enter

# Wait for a few seconds to see the search results

# Close the browser

In this script:

  1. We import the necessary modules from Selenium, including webdriver for the WebDriver, Keys for keyboard interactions, and time for adding delays.
  2. We create a new instance of the Chrome WebDriver using webdriver.Chrome().
  3. We navigate to the example website (https://www.example.com) using driver.get().
  4. We find the search box element by its name (assuming there’s a search box on the page with the name “q”).
  5. We type “Selenium WebDriver” into the search box and simulate pressing the Enter key.
  6. We wait for 5 seconds using time.sleep(5) to allow time for the search results to load (you might need to adjust this time based on the page you’re testing).
  7. Finally, we close the Chrome browser using driver.quit() to clean up resources.

You can customize this script to interact with different elements on a web page and perform various actions as needed for your web automation tasks.