I have selenium Test case to validate contents of 2 web pages. The issue, I am facing is I'm not able to get anything from element.getText().

I'm using below code.

driver1.get("https://some url");
String b = driver1.findElement(By.className("supArticleContent")).getText();
WebDriver driver2 = new ChromeDriver();  
driver2.get("https://some other url");
String a = driver2.findElement(By.className("supArticleContent")).getText();
System.out.println("text content is"+a);

if (a.equals(b)) {
     System.out.println("TC PASSED");          
     System.out.println("TC FAILED");
  • 1
    What is the HTML for the supArticleContent? Commented Jul 31, 2018 at 9:57
  • you should try some other locator instead of class name there are less chances for this to be unique on webpage
    – NarendraR
    Commented Jul 31, 2018 at 10:45
  • please fix your code formatting..
    – Alexey R.
    Commented Jul 31, 2018 at 12:12
  • It's also possible that you are confusing .getText() with .getAttribute("value") but I can't tell because of a lot of missing information. Commented Aug 1, 2018 at 13:22
  • 1
    @NarendraR, your comment reads like a good beginning to a good answer.
    – Malachi
    Commented Aug 9, 2018 at 18:19

2 Answers 2

  1. if html code is <div value="your expected text">

    try: element.getAttribute("value")

  2. if html code is <div>your expected text<div>

    try element.gettext(); or element.getAttribute("innerHTML");

    I met one fail case by gettext() but it worked by getAttribute("innerHTML")

div is just a tag name used for example, it can be any tag name as you may already know.


This can have following reasons:

  • Either the element cann't be located with the following locators you have given
  • In the second line you have mentioned driver2 for accessing the second website but you have mentioned driver1.findElement(By.className("supArticleContent")).getText();.

Try Syso after getting the text and storing in a variable. Then you will be able to debug this problem.

  • you may want to update your answer, the OP has edited their question to make use of driver2
    – Malachi
    Commented Aug 9, 2018 at 18:30

Your Answer

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

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