How to get text found between span in Selenium?
Last Updated :
03 Sep, 2024
Selenium WebDriver enables you to interact with web elements on a page. To extract text from a <span> element, you need first to locate it using one of Selenium’s locator strategies, such as by ID, class, or XPath. After locating the <span> element, you can retrieve its text content with a simple command, making data extraction straightforward.
How to create text found between span
Step 1: Create an HTML Page
Create a file called example.html and include the following content in it. This file will serve as the basis for your example, allowing you to demonstrate or test the code.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example Page</title>
</head>
<body>
<h1>Welcome to the GFG</h1>
<p>This is a sample paragraph.</p>
<span class="your-class-name">Sample Text!</span>
</body>
</html>
Step 2: Set Up Selenium WebDriver
Selenium is installed and your WebDriver, like ChromeDriver, is properly set up. This setup is necessary to run Selenium tests and automate interactions with your web browser.
pip install selenium
Step 3: Run the Python Script
Create a Python script named extract_text.py and include the following code. This script will allow you to run the necessary tasks or tests for your project.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize the WebDriver (Chrome in this case)
driver = webdriver.Chrome()
# Open the local HTML file
driver.get('file:///path/to/your/example.html') # Replace with the correct path to your HTML file
# Locate the <span> element by its attributes (e.g., class, id)
span_element = driver.find_element(By.XPATH, '//span[@class="your-class-name"]')
# Extract the text from the <span> element
text = span_element.text
# Print the text
print(text)
# Close the WebDriver
driver.quit()
Step 4: Run the Script
Run the Python script in your terminal or command prompt. This will execute the code and perform the tasks or tests you've written in the script.
Output:
Sample Text!
Conclusion
This example demonstrates using Selenium WebDriver to extract text from a <span> element in a local HTML file. Before running the script, ensure that your WebDriver, such as ChromeDriver, is correctly set up and that all file paths are accurate. This setup will help the script run smoothly and retrieve the desired text.
Similar Reads
Python Selenium - Find element by text The technique to verify if the requirements given by the user meet with the actual software product developed is known as Software Testing. Moreover, it also checks if the final software product developed is error-free or not. Software testing can either be performed manually or with the help of sof
3 min read
How to get text of a tag in selenium - Python? Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will w
1 min read
How to Get Child Element in Selenium? The tool that reduces human effort and makes the work relatively easier by automating the browser is known as Selenium. The elements which are located within some other elements, such elements are called child elements. Sometimes, we have to handle such elements, such as values of radio buttons, lis
5 min read
text element method - Selenium Python Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method â Selenium Python. Just bein
2 min read
Python Selenium - Find Button by text In this article, let's discuss how to find a button by text using selenium. See the below example to get an idea about the meaning of the finding button by text. Example: URL: https://html.com/tags/button/ We need to find the "CLICK ME!" button using the text "Click me!". Module Needed: Selenium: Th
2 min read