Chrome webdriver Python example script

Here’s an example script that demonstrates how to use the Chrome WebDriver in Python:

from selenium import webdriver
from import Service
from import By

# Path to the chromedriver executable
webdriver_service = Service('path/to/chromedriver')

# Launch Chrome using the webdriver
driver = webdriver.Chrome(service=webdriver_service)

# Navigate to a webpage

# Find an element on the webpage by ID and interact with it
element = driver.find_element(By.ID, 'my-element-id')

# Find an element by CSS selector and extract its text
element = driver.find_element(By.CSS_SELECTOR, 'h1.title')

# Close the browser

Make sure you have installed the Selenium package and have downloaded the Chrome WebDriver executable (chromedriver) compatible with your Chrome browser version. Adjust the path to the (chromedriver binary) accordingly in the line.

webdriver_service = Service('path/to/chromedriver')

This script launches Chrome, navigates to interacts with elements on the webpage, extracts text from an element, and then closes the browser using the driver.quit() method.

You can modify the script to suit your needs by adding more actions like filling forms, submitting forms, or navigating to other pages. This is a very basic example.

Related: Basic script to Write to a file using Selenium Webdriver and Python