| by Arround The Web | No comments

Selenium GetText Method to Retrieve an Element

In this article, we’ll learn how to use the Selenium WebDriver’s getText() method to retrieve an element’s text. We’ll learn how to retrieve a text from a website that might be contained in any web element. You will be fully capable of using the different applications of the getText() function that we will discuss to validate a text, read a text, and perform actions on the text. This article performs the basic task of obtaining the text of any web element.

In this tutorial, we use the paragraph tags. To do this, we perform several very important and necessary steps in Java JDK and Eclipse editor by utilizing the Selenium framework code for program execution. You will find these steps in the following sections.

GetText Method in Selenium

Selenium provides the GetText() method that can be used to retrieve the innerText values. The text that appears between a tag’s opening and closing is referred to as the “inner text.” As an example, innerText contains paragraphs, titles, etc. It snubs the white spaces of an element. To retrieve the innerText of the web element with the help of the getText() method, we need to follow these steps:

  1. First, you need to identify the particular element and its child by locators such as class, name, id, and XPath.
  2. After that, use the getText() method of Selenium to retrieve the data value of that particular element.

Now, let’s try to understand the working example of the GetText() method through a detailed example given in the next section.

Example:

In this instance, we use the “Omy organic” website. We will learn how to retrieve the paragraph text and verify it. To perform such action, we use the getText() method to retrieve the innerText of a particular web element.

Let us verify the getText() method to check the innerText of the tag

and its sub-elements. To perform such task, configure the TestNG jars and set dependencies first.

All of the required steps are mentioned with proper commands and explanations for your help.

Step 1: Browse the Website to be Used

Consider the page located at https://www.omyorganic.com/ to use for this example. You can use any other website that you want when you execute this example on your system.

The website illustration is shown in the following figure:

In the provided website page, we target the paragraph that is entitled “Don’t Panic It’s Organic”.

Step 2: Import the WebDiverManager

The next step requires you to import the WebDriverManager. It works as an API in Selenium that allows automating the management of the Selenium WebDrivers’ various versions compatibility such as Chromedriver, Getkodriver, and Firefox. It saves time by not downloading and adding the libraries of the WebDrivers’ latest version.

The following script is used to import the WebDriverManager in our example:

import io.github.bonigarcia.wdm.WebDriverManager;

Now, let’s move on to the next step which is the setup call for the WebDriver. It is a vital step that must be taken and shouldn’t be skipped.

Step 3: Setup a Call for the WebDriver

In this step, we use a Chrome browser to perform our task. First, we download the Chrome drivers according to our Chrome browser version. Then, we set up the browser drivers using the WebDriverManager. The code is given in the following:

WebDriverManager.chromedriver().setup();

To create an interaction between Selenium and the browser, we need a WebDriver setup. The syntax for that is mentioned in the following:

ChromeDriver myobject = new ChromeDriver()

Now, we need to create an object of Chrome drivers to use it using the following command:

ChromeDriver mydriver = new ChromeDriver();

Step 4: Open the URL with the Automatic Control Test

In this step, we open the Chrome browser using the get() function which launches the Chrome browser and opens the given https://www.omyorganic.com/ site website URL.

The given URL is opened in the current browser window via the get() method. The URL must start with https://www.abc.com (for example).

The syntax to open the URL is shown in the following:

mydriver.get(URL string)

Use the following script method:

mydriver.get("https://www.omyorganic.com/");

Step 5: Select the <p> WebElement

Browse the site https://www.omyorganic.com/ > Right click > select inspect option. You will find the inspect window. Then, click and inspect on the webElement as highlighted in the following to get the locator of that paragraph:

After inspecting the highlighted paragraph, you will see the window as shown in the following:

You might notice here that <p></p> is inside the paragraph tag. There are also sub-elements like <br>. Here, we should only pick up the Xpath of the <p></p> tag.

Step 6: Identify the WebElement

To identify the element, find its Xpath first. As we have seen in the previously-inspected code tag, <p></p> doesn’t have any id, class, or name. So, for that to identify it as a unique element, we get its Xpath.

The following steps must be followed to obtain the Xpath:

Go to https://www.omyorganic.com/ > right click > select inspect option> right click on <p> tag > go to copy option > and select Xpath.

Now, we get the Xpath of the <p></p> element.

Here, you can also look at the following syntax:

WebElement myvariable = drive.findElement(By.xpath(xpath))

FindElement() is the method in Selenium which allows access to the web elements on the web page. Its syntax is given as follows:

findElement(By.locator)

We also attached an example code for your reference:

WebElement mypp=driver.findElement(By.xpath("//*[@id="shopify-section-16482047088e8db06e"]/div/div/div/div/div/p[2]"));

If you find an error in the WebElement, hover it and import the WebElement Selenium library class.

Step 7: Obtain the Text from the Site

We use the getText() method to obtain or retrieve the text from a site. The syntax for that purpose is given as follows:

String myvariable = field.getText()

The obtained text should be in String format. The “myvariable” variable saves the text from the <p> tag using the getText() method. Here is an illustration of the code:

String s= p.getText();

Step 8: Show the Obtained Text in the Console

Now, all we need is to print out the result that is saved in the “s” variable which is retrieved by the getText() method. Here is the code that is executed:

System.out.println("Text content is : " + s);

Step 9: Close the Browser Automatically

After retrieving text from the website, close the browser automatically using the close() method. Here is how you can do this:

driver.close();

We also provided the complete coding here for your help:

package ui;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class GetTextElement {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
ChromeDriver mydriver = new ChromeDriver();
mydriver.get("https://www.omyorganic.com/");
WebElement p=mydriver.findElement(By.xpath("//*[@id="shopify-section-16482047088e8db06e"]/div/div/div/div/div/p[2]"));
String s= p.getText();
System.out.println("Text content is : " + s);
driver.close();
}
}

In the previous code example, we used the getText() method to retrieve the text from the website.

When the script is executed, the paragraph <p> tag data is automatically retrieved as shown in the following console output:

Conclusion

We give a brief description of the getText() method which is used to fetch the innerText data of an element and its child. It is helpful for error verification. In this context, we learned about the getText() method and how it works using the Selenium framework to obtain the text form through an automatic control tool. In the end, we demonstrated the results on the console which shows that the getText() method obtained the paragraph data successfully. This article includes all the required information regarding the getText() method which you can follow if you are a beginner.

Share Button

Source: linuxhint.com

Leave a Reply