1

HTML Code:

<div class="deviceName truncate"><a ng-href="#" href="#" style="">Hello  World</a></div>

Each <a> element, the Link text contains double space as "Hello World"

Retrieving information, in List

List<WebElement> findAllUserName = driver.findElements
                (By.xpath("//div[@class='deviceName truncate']//a[text()]"));

        for (WebElement webElement : findAllUserName) {
            String findUSerText = webElement.getText();
            System.out.println(findUSerText);
        }

Its gives list Result with single space, "Hello World"

How should overcome to this situation ? To compare text,

Concern behind this, wants to compare list element with given string :

driver.findElement(By.xpath("//div[@class='deviceName truncate']//a[contains(text(),'" + name + "')]"))

And its considering double space,

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
  • `Hello World` in HTML does have 2 spaces, are you geting only 1 space in the output `Hello World`? Perhaps I am not that confident with the _xpath_ `//div[@class='deviceName truncate']//a[text()]` – undetected Selenium Apr 06 '18 at 06:28
  • xpath is working correctly, and giving result. One of the xpath tool ChroPath also giving same result with Single space. – Ishita Shah Apr 06 '18 at 06:29
  • So where are you stuck? Is there a question for us? – undetected Selenium Apr 06 '18 at 06:30
  • Yes, while comparing string with same xpath its considering double space. The reason I know why web page is considering single page and why HTML code considering double space. While comparing text its considering HTML code which has double space. I am looking for solution, – Ishita Shah Apr 06 '18 at 06:34
  • What solution did you want? That the comparison treats single space as equal to double space, or that getText() returns the string without collapsing the double space to a single space? – Alohci Apr 06 '18 at 07:50
  • getText() returns Single space, Solution where we can manually pass string by filtering word space, And it can consider single space any how in between text, Is there any suitable method ? – Ishita Shah Apr 06 '18 at 08:59

1 Answers1

0

If you just check how you browser renders the link you will realize that it is also having a single space. See this discussion: Do browsers remove whitespace in between text of any html tag

So you either should to add a style to your page like this

a {
   white-space: pre; 
}

That will make all your links unformatted.

or try to inject the style of the particular element on the fly like it is shown here: How can i set new style of element using selenium web-driver

Here is the example having two identical links but with different styles set.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • Thanks @alexey for your suggestion, But We QA don't have control on Web Pages CSS. I think it needs to get solve from back end itself. – Ishita Shah Apr 10 '18 at 05:16
  • As I said you can try to inject css when you run Selenium script. You can also reconsider your approach. Since you should test what user should see, then you can collapse whitespaces in your assert since as I have mentioned the user will see the whitespaces collapsed in their browser unless the special style is assined to a link. – Alexey R. Apr 10 '18 at 09:43