Here’s an example script that demonstrates how to use the Chrome WebDriver in Python:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by 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
driver.get('https://codetryout.com')
# Find an element on the webpage by ID and interact with it
element = driver.find_element(By.ID, 'my-element-id')
element.click()
# Find an element by CSS selector and extract its text
element = driver.find_element(By.CSS_SELECTOR, 'h1.title')
print(element.text)
# Close the browser
driver.quit()
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 https://codetryout.com/ 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