-1
<a href="editauthority.jsp" title="Add a new connection" class="link btn btn-primary" role="button">
    <i class="fa fa-plus-circle fa-fw" aria-hidden="true"></i>
Add a new connection</a>

I have an HTML element as above with an font-awesome icon. I want to find an element from html using its text.

This is what I have tried so far

//a[contains(@class,'btn') and contains(normalize-space(text()),'Add a new connection')]

But the problem is //a[contains(@class,'btn') and contains(normalize-space(text()),'')] returns the expected value with a new line before and few space at the start, which doesn't match with the expected value of Add a new connection

How can I match the element text ignore all new line and extra spaces.

I am using the above XPATH to find elements in selenium testing.

Update

Adding Selenium code

/**
* Clicks a button based on visible text, this type of button is created using anchor tag with .btn class
* @param text
*/
public void clickButton(String text)
{
    WebElement element =
        waitElementClickable(
            By.xpath("//a[contains(@class,'btn') and contains(normalize-space(text()),'" + text + "')]"));
    element.click();

    if (!isAlertPresent())
    {
      wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loader")));
    }
}
Kishore Kumar
  • 11,843
  • 26
  • 86
  • 149
  • Oh, so you want just to select element? I thought you need to extract text... this part about `XPath` that `"returns the expected value"` is confusing :) – Andersson Mar 21 '17 at 21:21
  • Have you tried something simple like `By.linkText("Add a new connection")` or `By.partialLinkText("Add a new connection")`? One or both of them should work without all the XPath. – JeffC Mar 22 '17 at 01:33

2 Answers2

3

It is important to realize the difference between Testing text() nodes vs string values in XPath.

Change this clause,

contains(normalize-space(text()),'Add a new connection')

to this

normalize-space()='Add a new connection'

So your complete XPath would read,

//a[contains(@class,'btn') and normalize-space()='Add a new connection']

You might also want to avoid inadvertent substring matching on @class by using this technique.

Community
  • 1
  • 1
kjhughes
  • 89,675
  • 16
  • 141
  • 199
  • The above code doesn't code when there a ` ` along with text in the prefix – Kishore Kumar Mar 21 '17 at 21:22
  • Your question said *nothing* about ` `, which presents its own set of issues and merits its own question. If this answer helps you with the question you asked, then please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer and ask a new question about normalizing away ` `. Thanks. – kjhughes Mar 21 '17 at 22:57
0

You can try this expression:

//a[contains(@class, "btn") and contains(.,"Add a new connection")]

Example

Vlad
  • 308
  • 3
  • 10
  • The problem with this is, it will also find match any button that starts with Add – Kishore Kumar Mar 21 '17 at 22:16
  • You can more specific point to a needed button e.g. change `a[contains(@class, "btn")` to `a[@class="link btn btn-primary"]` – Vlad Mar 21 '17 at 22:32