English translation
Create a Chrome browser instance
AI Article Decision Snapshot
Turn the lesson into workflow, model, budget, and security checks before choosing tools.
Use this quick snapshot before leaving the article. It keeps the next search tied to practical AI software, model/API, cost, privacy, and implementation questions.
Workflow fit
Identify the real job behind the article: coding, research, document review, support, analytics, content, or internal automation.
Model or tool decision
Decide whether the next step is a software shortlist, an AI tool comparison, an API platform choice, or a model benchmark.
Budget and usage signal
Estimate seats, API calls, prompt volume, retries, review time, and fallback work before assuming the workflow is cheap.
Security and privacy review
Check whether source code, customer data, private documents, prompts, logs, or embeddings will enter the AI workflow.
In the previous tutorial, we thoroughly covered how to install and configure Selenium. Now, we officially begin exploring basic web automation operations. In this tutorial, you’ll learn how to perform fundamental web interactions using Selenium, such as opening a webpage, locating elements, entering text, and clicking buttons. We’ll walk through practical examples with clear code snippets to help you grasp each concept effectively.
Overview of Basic Operations
When performing web automation with Selenium, you typically need to master the following core operations:
- Launching a browser and navigating to a webpage
- Locating web elements
- Interacting with web elements
- Closing the browser
We’ll now cover each of these operations in detail.
1. Launching a Browser and Opening a Webpage
First, import the Selenium library and select a browser driver—in this example, we use Google Chrome:
from selenium import webdriver
# Create a Chrome browser instance
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://www.example.com')
Here, we import the webdriver module and instantiate a Chrome browser. The get() method loads the specified URL.
2. Locating Web Elements
Web elements can be located in multiple ways—by ID, name, class name, tag name, and more. Below are some commonly used locator strategies:
# Locate element by ID
element_by_id = driver.find_element(By.ID, 'example_id')
# Locate element by name
element_by_name = driver.find_element(By.NAME, 'example_name')
# Locate element by class name
element_by_class = driver.find_element(By.CLASS_NAME, 'example_class')
# Locate element by tag name
element_by_tag = driver.find_element(By.TAG_NAME, 'h1')
⚠️ Note: The legacy methods like
find_element_by_id()have been deprecated since Selenium 4.0. Usefind_element(By.<METHOD>, 'value')instead (as shown above). Ensure the target element is present and loaded before attempting to locate it.
3. Interacting with Web Elements
Once an element is located, you can interact with it. Common interactions include typing text, clicking buttons, and retrieving element text.
Entering Text
To type into an input field, use the send_keys() method:
# Locate an input field and enter text
input_box = driver.find_element(By.NAME, 'username')
input_box.send_keys('my_username')
Clicking a Button
To simulate a click on a button or link, call the click() method:
# Locate and click the login button
login_button = driver.find_element(By.NAME, 'login')
login_button.click()
Retrieving Element Text
To fetch visible text from an element—or retrieve the page title—use the .text property or built-in attributes:
# Get the page title
page_title = driver.title
print(page_title)
# Get visible text of an element
heading = driver.find_element(By.TAG_NAME, 'h1').text
print(heading)
4. Closing the Browser
After completing your automation tasks, always close the browser to release system resources:
# Quit the browser session
driver.quit()
✅
driver.quit()closes all associated windows and ends the WebDriver session. Prefer it overdriver.close(), which only closes the current window.
Practical Example
Below is a complete, working example: automating login on a sample login page—entering credentials and submitting the form.
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# Create a Chrome browser instance
driver = webdriver.Chrome()
try:
# Open the login page
driver.get('https://www.example.com/login')
# Enter username and password
driver.find_element(By.NAME, 'username').send_keys('my_username')
driver.find_element(By.NAME, 'password').send_keys('my_password')
# Click the login button
driver.find_element(By.NAME, 'login').click()
# Pause briefly to observe the result (optional)
sleep(5)
finally:
# Always quit the driver to clean up resources
driver.quit()
In this example, we navigate to a hypothetical login page, populate the username and password fields, submit the form, pause for observation, and safely terminate the session. The try...finally block ensures the browser closes even if an error occurs.
Summary
In this tutorial, we explored essential Selenium web automation techniques: launching a browser, navigating to pages, locating elements using modern By locators, interacting with elements (typing, clicking, reading text), and properly closing the browser. These foundational skills form the bedrock for tackling more advanced automation scenarios.
Next, we’ll dive into advanced operations, including handling dynamic content (e.g., AJAX-loaded elements, waiting for conditions, managing pop-ups and frames). Stay tuned for our upcoming tutorials!
Apply This Lesson
Turn this article into AI software, model, API, and security decisions.
English Article FAQ
Use this article as evidence before choosing AI tools
How should I use this AI Tutorials article?
Use it as the implementation or learning layer, then connect the idea to AI software buyer guides, tool comparisons, benchmarks, API choices, and security checks before making a production decision.
Is this English article different from the Chinese original?
The English edition is localized for global AI readers while preserving the original diagrams, screenshots, prompts, code examples, and source context from the Chinese article.
What should I read after Create a Chrome browser instance?
Continue with AI Software Buyer Guides, AI Tools Workbench, Best AI Coding Agents, AI Model Benchmarks, OpenAI vs Anthropic API, or LLM Security Tools depending on the decision you need to make.
Can this article alone choose an AI product or model?
No. Treat the article as evidence and context, then validate fit with pricing, privacy requirements, integration effort, benchmark results, workflow tests, and fallback planning.
Continue