Search code examples
javaseleniumxpathnormalize

Using xpath containing whitespace


List <WebElement> tableSituacao = driver.findElements(By.xpath("//*[@id=\"tabela\"]/tbody/tr/td[5][contains(text(),"+situacao+")]"));

how to use normalize-space in my case? "situacao" has white spaces


Solution

  • There's no need to handle this with XPath. You can just .trim() the string.

    List <WebElement> tableSituacao = driver.findElements(By.xpath("//*[@id='tabela']/tbody/tr/td[5][contains(text(),'" + situacao.trim() + "')]"));
    

    I would also use ' inside the XPath instead of escaping the double quotes \". I think it makes it easier to read.