Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda