yanayeay
yanayeay

Reputation: 31

Unable to click link that is having   using Capybara

I have found the xpath for the link, however the link cannot be click.

<a href="#" onclick="submitLink("89502", "6022955204"); return false">01 &nbsp; 490508125096</a>

link = bosession.find(:xpath, "//a[contains(.,'490508125096')]").text
puts link 
bosession.click_link link

for puts link I have the output as 01 490508125096

as for click_link I have the error ElementNotFound : Unable to find link or button "01 490508125096"

Is it because the link having &nbsp; that causing the link cannot be found?

The same method I used for other page, and I'm able to click.

<a href="#" onclick="submitLink("3854"); return false">1234232</a>

link = bosession.find(:xpath, "//a[contains(.,'1234232')]").text
bosession.click_link link

Upvotes: 1

Views: 332

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

It's happening because the locator passed to click_link is compared directly against the document contents in XPath, but by getting the text you're actually getting the as shown contents (2 spaces) vs the actual contents (normal space, nbsp). However, there really is no need to find the link by your own xpath and then click via the text string you get from that. Rather you can just click via the substring in the first place (assuming default configuration)

bosession.click_link '490508125096' # will click based on partial string

If you really want to do the full string you could do

bosession.click_link "01 \u00a0490508125096", exact: true # \u00a0 for the nbsp

You could also do

bosession.click_link text: "01  490508125096" # could use exact_text: here instead of text: if you want an exact match rather than substring

which works a little differently. As explained previously when you're passing the locator (just a string) it is compared, via XPath, to the exact contents of the document. However when you pass the :text option it is compared to the text as shown/normalized by the browser. Doing that is a bit less efficient though because it involves finding all links on the page, getting the "as shown" text and then comparing.

Upvotes: 2

Related Questions