Selenium Getting Text and other values from an Element

Selenium Getting Text and other values from an Element

Getting text from an element in selenium is very useful for a few reasons. To name one, it is useful for creating a condition to give your test a pass/fail criteria. I.e. if you expect some specific text to be displayed after a successful run of your case.

To name some other uses: Text can be grabbed from one area and used in another input. In some rare cases, there is so little information available to locate an element that text is the best thing to use to find it (Do not use this approach unless you have to, as in a lot of sites text is changed quite frequently).


Getting text from an element

To get text from an element in Selenium Java, use the method .getText() like so:

WebElement languageLinkEnglish = driver.findElement(By.xpath("//a[@id='js-link-box-en']//small"));

String numberOfArticles = languageLinkEnglish.getText();

This code snippet will find and retrieve the text detailing how many Wikipedia articles are written in english.

If you want to see a case containing this snippet in action, click here:

Run Working Example



Getting other values from elements

Sometimes, text is either not relevant or not present. There are many other options for the values you can get from elements.

Getting Attributes from Elements

Attributes are properties used by most HTML tags (or page elements as we refer to them in testing) that can be seen by inspecting the element in question (right-click and press "inspect" if you are using chrome). An example of an element attributes can be seen in this example html code:

<a
    id="js-link-box-en"
    href="//en.wikipedia.org/"
    title="English — Wikipedia — The Free Encyclopedia"
    class="link-box"
    data-slogan="The Free Encyclopedia">
</a>

The attributes you can see here are: id, href, title, class and data-slogan. To retrieve any one of these values as a String, use the method .getAttribute("attributeName"), replacing attributeName with the attribute you want to retrieve. E.g:


WebElement languageLinkEnglish = driver.findElement(By.xpath("//a[@id='js-link-box-en']"));
String hyperlink = languageLinkEnglish.getAttribute("href");

This code snippet will find and store the hyperlink to the Wikipedia main page by copying the href attribute within the english language option found on Wikipedia's home page.

The following link will take you to an example case that uses this code snippet to verify that a hyperlink is correct.

Run Working Example



Getting visual (CSS) properties from Elements

Sometimes your test scenario will require retrieving a visual characteristic to make sure the user is seeing the page as the developers intended. These visual properties are all stored in the page as CSS properties. Examples include, text/background color, size, font, etc.

In order to retrieve a specific CSS property, you will need to look up exactly what the CSS value is called and use the method element.getCssValue("propertyName"), where propertyName is the exact name of the CSS value you found. E.g:

WebElement languageLinkEnglish = driver.findElement(By.xpath("//a[@id='js-link-box-en']//strong"));

actionsDriver.moveToElement(languageLinkEnglish).build().perform();

String hoverColorValue = languageLinkEnglish.getCssValue("background-color");

This code snippet uses the Actions class to hover over a language link on wikipedia's homepage and then retrieves the value of the background color (which should be grey now).

This code snippet is used in the following example, where the linked case retrieves the background colour of the link before and after hovering in order to check that the hover effect is shown as intended.

Run Working Example



The full list of the methods belonging to WebElement can be found here:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebElement.html

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.