Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
String comparisons in selenium using xpath fails even though both strings are exactly same

I am using key word driven and data driven framework. Expected string is coming from excel sheet and Actual from webpage even though both variable print exactly same strings still the Test script fails. It does "PASS" for strings with no space like "Resign" but "FAIL" for string with space like "Property Search" below is the result which prints PASS till "Resign" after that it fails ignore the numbers its for debugging. I also included xpath used in result.

Result generated
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[1]
Tool tip text present :- Revals
Revals
1 By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[2]/a[contains(text(),'Managed Client Accounts')]
Tool tip text present :- Managed Client Accounts
Managed Client Accounts
1
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[3]
Tool tip text present :- Resigns
Resigns
1
By.xpath: .//[@id='dashboard-dropdown']/li[4]/a
Tool tip text present :- Outstanding AR
Outstanding AR
4
By.xpath: .//
[@id='menuCreateDashboard']
Tool tip text present :- Manage Dashboard
Manage Dashboard
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[2]/a
Tool tip text present :- Property Search
Property Search
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[3]/a
Tool tip text present :- Tax Resource Database
Tax Resource Database
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[4]/a[contains(text(),'Quick Links')]
Tool tip text present :- Quick Links
Quick Links
4
Here is the code:

public String verify_Text(String locatorType, String value, String data){
       try
  {
     By locator;
     locator = locatorValue(locatorType, value);
     System.out.println(locator);
     WebElement element = driver.findElement(locator);


     //WebElement element = driver.findElement(By.cssSelector("#header>h1 a"));
    // Get tooltip text
    String toolTipText = element.getText();
    System.out.println("Tool tip text present :- " + toolTipText);
    System.out.println(data);

    // Compare toll tip text

         if(toolTipText.contentEquals(data))
           {
               System.out.println("1");
              return PASS;

           }



  }
  catch(Exception e)
  {
     LOG.error(Executor.currentSheet + ":" + e);
     getScreenshot("verify_Link", data);
     System.out.println("3");
     return FAIL;
  }
  getScreenshot("verify_Link", data);
  System.out.println("4");
  return FAIL;

}

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

line of code which worked for me

if(toolTipText.replaceAll("\\s+","").equalsIgnoreCase(data.trim().replaceAll("\\s+","")))

I realized after using this code and debugging is data passed from excel sheet had space in between two letter whereas there was no space on webelement tool tip .But this I was not able to figure it out using inspect element as its not visible. before using replaceall("\s+","")

By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[5]/a Tool tip text present :- ManageDashboard Manage Dashboard

after using replaceAll("\s+","") and removing space from data coming from excel sheet

By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[5]/a Tool tip text present :- ManageDashboard ManageDashboard

over 4 years ago · Santiago Trujillo Report

0

I don't have enough rep to comment but I am suggesting some things to try:

You could trim the string, perhaps there is a char that is not visible in one of the Strings.

if(toolTipText.trim().contentEquals(data.trim()))

Or you could try to use normalize-space to remove tab, carriage return and line feed characters.

/*/a[normalize-space() = 'Hello World']
over 4 years ago · Santiago Trujillo Report

0

Sometimes when you fetch the text from WebElements, they tend to have a leading or trailing space(s) in them. A better comparison would be

toolTipText.trim().equals(data.trim())

If this still doesn't work, maybe remove all the spaces from the strings and then compare. Like this:

toolTipText = toolTipText.trim().replaceAll("\\p{Z}", "");
data = data.trim().replaceAll("\\p{Z}", "");
toolTipText.equals(data)

Here, the string Outstanding AR will become OutstandingAR after replaceAll. This is certainly an overkill, but if it works, then it's evident that the spaces in the string is what caused the problem.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!