How To Get Text Of An Element In Selenium?

Sonu Kumar Deo

Posted On: April 4, 2024

view count394030 Views

Read time12 Min Read

Selenium is the most widely used automation testing tool, which reduces human effort and efficiently handles testing the scenarios we encounter every day. One such scenario is how to get the text of an element in Selenium. Selenium offers a getText() method used to get the text of an element, i.e.; it can be used to read text values of an element from a web page.

In this article, we will understand how to get the text of an element using the getText() method offered by Selenium WebDriver in Java and Python. We will understand how we can fetch text from a webpage that might be present in any of the web-element. We will learn various applications of the getText() method. By the end of this article, you will be fully capable of validating the text or even reading and performing actions on text using Selenium WebDriver.

getText() Method in Selenium

In Selenium, the getText() method extracts the visible text from a WebElement, returning it as a string while trimming any leading or trailing whitespace.

This method allows easy access to innerText unaffected by CSS styles. Unlike alternative techniques, getText() exclusively captures user-visible content on web pages, serving as a critical function for tasks like web scraping and data extraction.

Syntax of the Selenium getText() method is different in different programming languages. Let’s see how to use this method in Java and Python.

Selenium get text method in Java:

In Java, Selenium provides us with getText() method to get innerText of the selected WebElement.

Selenium get text method in Python:

In Python, we can use web_element.text to get inner text of the selected WebElement as shown in the bellow code snippet.

Let us learn about these terms elaborately one by one:

innerText in HTML

Inner Text refers to the text that appears between opening and closing of any tag. For example, consider the below HTML code snippet:

innerText in HTML

Saving the above code as HTML (.html file) would open a window with contents like below:

html file

So here the texts, “Let us begin!”, “Hello World..”, and “Click Me!” are the texts that come between the HTML tags. Hence these are the innerText of the respective WebElements in which they are wrapped.

Not Hidden By CSS

There is one more term mentioned in the definition above, i.e., not hidden by CSS. Let us understand that too. Consider the following HTML snippet-

Not Hidden By CSS

In the code above, the text inside the < span > tag will not be visible on the UI as it is hidden by CSS.

html code

Now that you know what inner text and hidden by CSS means, let us iterate our definition which says, getText() method in Selenium fetches the innerText of a WebElement, which is not hidden by CSS and returns it as a String value. In simple words, whatever is displayed as text on the browser will be returned as-is by the getText() method. If there is no text corresponding to a web element, an empty string is returned.

On a side note, to make your Selenium automation process even more smoother, you can also leverage AI test assistants such as KaneAI.

KaneAI is a smart AI test agent that empowers teams to create, debug, and refine tests using natural language. Designed specifically for high-speed quality engineering teams, it seamlessly integrates with LambdaTest’s suite of tools for test execution, orchestration, and analysis.

Selenium Get Text Method To Get Heading Or Paragraph

Till now, we have seen how to get text of a WebElement from a web page. Let us now begin with the practical application of the getText() method. Before reading the text, it is essential to use one of the widely used Selenium Locators for locating the WebElement.

We all know that no website is complete without headings or paragraph content. At times it becomes necessary to verify the text on the web page we are landing on. The getText() method in Selenium helps us retrieve a text and do necessary action on it. In the code below, we are reading the heading text from the web page, comparing it with the expected value, and then printing the results. We will be using LambdaTest’s ECommerce demo website to practice this automation example.

In our example, we will try to get the text from a paragraph in the website as highlighted in the below image.

selenium playground

HTML source code of the highlighted text

HTML source code

Java Code

In the above code, we open the URL(https://ecommerce-playground.lambdatest.io/), find the div with ID “entry_217838“, locate the nested < p > tag, and further identify the < strong > tag within it. Using Selenium’s getText() method in Java, we extract and print the text content from the selected strongElement. The code ensures accurate text retrieval from the specified HTML hierarchy in a concise and readable manner.

Python Code

In the above code, we navigate to the URL(https://ecommerce-playground.lambdatest.io/), locate the div with ID “entry_217838,” then drill down to the nested < p > and < strong > tags using Selenium. Finally, by extracting text content with text attribute, we print the information, ensuring accurate retrieval from the specified HTML structure in a concise Python script.

There are multiple cases when we need to select a specific dropdown value, or maybe get the text values in a dropdown. We can use the getText() method to handle such scenarios as well. We will now see how we can get the values in a dropdown and print the same in the console. To run the script for dropdown, we have used the same demo website.

In this example we wil open the dropdown menu and print all the category titles in each row.

dropdown menu

HTML source code of the dropdown

HTML source code of the dropdown

Java Code

In the above code, we open the demo website, locate the mega menu’s parent div with ID “entry281_216475” using Selenium findElements() method. Now, by finding all H3 tags within this div, we iterate through each H3 element, extracting and printing their text using getText() method in Selenium Java. This process retrieves and displays the text of each category in the mega menu.

Python Code

In the above Python code, we open the demo website, locate the mega menu’s parent div with ID “entry281_216475” using Selenium find_element_by_id() method. By finding all H3 tags within this div, we iterate through each H3 element, extracting and printing their text using h3_tag.text. This process retrieves and displays the text of each category in the mega menu usign Python.

Using getText() Method To Get Alert Text

Just like the paragraph and dropdown, one can retrieve the text of an alert as well. Alerts are common with many applications, and hence there can be a need to check text present on it. The below code would fetch text from an alert and print it for this demo website.

Java code:

The AlertText class in Java is a test class that uses the Selenium WebDriver library to automate interactions with a web browser. The setUp() method initializes a new instance of ChromeDriver before each test, the alertText() method tests the alert functionality by navigating to a webpage, clicking a button to trigger an alert, retrieving and printing the alert text, and the tearDown() method closes the browser after each test. This class serves as a test suite for verifying the behavior of alerts on a webpage through automated testing, ensuring that the alert functionality functions correctly in a web application.

Python Code:

The provided code sets up a Selenium webdriver using Chrome, navigates to a webpage, triggers an alert by clicking a button, retrieves the text from the alert, prints it, and then tears down the webdriver. The test_alert_text function specifically finds and clicks a button to trigger an alert, switches to the alert, retrieves the text, and prints it. The setUp function initializes the Chrome webdriver, while the tearDown function closes the driver. This code snippet focuses on interacting with alerts using Selenium in a controlled browser environment.

Difference Between getText() And getAttribute() In Selenium WebDriver

In Selenium WebDriver, both the getText() and getAttribute() methods are used to retrieve data from an HTML element. Hence, developers genrally get confused over thefunctinality of these two methods. Note that both these methods are entirely different and return different values.

getText()

It returns the inner text of a given element. Inner text is the text between the opening and closing tags of the element. The getText() function only works when the specific element is visible on the webpage. It ignores all the leading and trailing spaces.

getAttribute()

The getAttribute() method returns the value of a given HTML attribute. It returns the value of the HTML element’s attribute as a string. If the attribute does not exist, it returns null.

Info Note

Simplify Testing: Opt for LambdaTest’s Selenium cloud grid with 3000+ real browsers and OS options. Try LambdaTest Today!

Execute Your Tests In Parallel Using Cloud Based Selenium Grid

Using a cloud-based platform is the best way to leverage Selenium automation and get the desired results seamlessly without worrying about installation and setup. LambdaTest is an AI-Powered Test Orchestration platform, set up as a cloud infrastructure. We can perform web automation on LamdaTest’s Selenium based cloud grid.

To do so, you can get the value for the access token, username, and gridUrl upon registering with LambdaTest. To register on LambdaTest, follow the register page url for free. After registration, you can find these credentials from the LambdaTest dashboard, as shown in the image below.

Once you have the credentials, you can declare your testing environment’s preferred capabilities. LambdaTest has Desired Capabilities Generator that will provide you with the capabilities based on your selection of OS, browser version, and resolution. Below is an automation test script to get text of an element in Selenium with Python using LambdaTest.

This is the only method that needs to be added or changed when executing your automation test script in LamdaTest’s cloud Selenium Grid. Mainly this function defines the desired capabilities of the system for testing. It also takes your_username and your_accesstoken obtained from LambdaTest. Lastly, this function uses these details to create the browser driver using webdriver.Remote() method.

Once this test is executed successfully. We can track our test case on the LambaTest dashboard as shown in the image below.

dashboard

Conclusion

In conclusion, this blog empowers you to leverage Selenium WebDriver’s getText() method to effortlessly extract text from web elements. You’ve learned its core function, syntax variations in Java and Python, and practical applications. We’ve differentiated getText() from getAttribute() to ensure clarity. Finally, explore how LambdaTest’s cloud grid streamlines your Selenium tests with getText(), enabling parallel execution for maximized automation efficiency. By mastering getText(), you’ll enhance your web scraping and data extraction capabilities in Selenium WebDriver.

Additionally, We came to realize the pain point of running Selenium WebDriver, & Grid, locally. Moving to cloud based Selenium Grid such as LambdaTest will help you scale effortlessly, so you could reduce your build times significantly & ship products faster.

Frequently Asked Questions (FAQs)

What is the getText() method in Selenium WebDriver used for?

The getText() method is used to extract the inner text content of a WebElement on a webpage. This text is what is displayed to the user and excludes hidden elements or leading/trailing whitespaces.

How does getText() differ from getAttribute() in Selenium?

While both methods retrieve data from web elements, they target different aspects. getText() focuses on the visible inner text, whereas getAttribute() fetches the value of a specific HTML attribute associated with the element.

Author Profile Author Profile Author Profile

Author’s Profile

Sonu Kumar Deo

Sonu Kumar Deo stands out as a skilled software developer adept in problem-solving, web development, and blockchain. Eager to explore emerging tech, he's dedicated to building efficient solutions. Beyond coding, Sonu excels in articulating ideas through exceptional writing and blogging, fostering collaboration, and seeking feedback to continuously evolve and contribute to the tech sphere.

Blogs: 4



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free