0

When I tried to get text of particular element which is returning all small letters. Hence my assertion has failed. kindly help me to get actual innertext and please let me know is there any other methods available to validate case sensitive inner text.

Error:

java.lang.AssertionError: expected [All content one platform] but found [all content one platform]

Code:

@FindBy(xpath ="//*[@class='promo__info']/h2")
WebElement AllContent;
String contentText = AllContent.getText();
    System.out.println("***********************" + contentText);
    Assert.assertEquals(contentText,"All content one platform");

Html:

<h2 data-v-31d5644f="">
      All content one platform
    </h2>
4
  • Which version you are using? Aug 10, 2022 at 12:54
  • What is rendered on the page actually? You may have capitalized text in source but client side scripts (for example) could change the case. So getText actually returnes what is rendered rather than what's in the sources.
    – Alexey R.
    Aug 10, 2022 at 15:35
  • I'm using selenium testng 7.5. Aug 11, 2022 at 6:25
  • As mentioned by Alexey R. it can be an issue with capitalized text. Share your CSS and possibly a screenshot of what exactly is displaying in your web page as your browser's console to further understand and help you. Aug 19, 2022 at 5:17

3 Answers 3

0

getText() :

  • This selenium method is working as expected for me and returned exact text instead of all text in lower case enter image description here

  • You can see text printed is actual text returned.

  • Another way to try using .getAttribute("value") This might not intended for same purpose as your field is not inout field, but can give a try for

0

There are multiple workarounds to handle such scenarios while doing web automation using Selenium. While working in software functional testing and frameworks in multiple languages below are the 02 top approaches widely used -

Method 1 - Use Java Inbuilt methods

Step 1 - Fetch the text using getText() method

Step 2 - Compare it with required string using Java's in built methods like equalsIgnoreCase() and store result as a boolean

Step 3 - Assert the boolean value using TestNg's Assert.assertTrue() method.

Method 2 - getAttribute() method of Selenium Now here try with conventional "value" attribute and if it doesn't work, try with all other attributes which gives string output in required format.

0

The getText() method in Selenium WebDriver retrieves the inner text of a web element and returns it as a string. By default, it returns the text in the same case as it appears on the page, but some browsers or elements may return text in lowercase.

To resolve this issue, you can use the getAttribute("innerHTML") method instead of getText(). This method retrieves the raw HTML content of an element, preserving the original case of the text.

Here is an example of how you can use the getAttribute("innerHTML") method to retrieve the text in its original case:

@FindBy(xpath ="//*[@class='promo__info']/h2")
WebElement AllContent;
String contentText = AllContent.getAttribute("innerHTML");
System.out.println("***********************" + contentText);
Assert.assertEquals(contentText,"All content one platform");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.