andgursky andgursky - 1 year ago 141
Ruby Question

how to get data element by title index with capybara "find"?

I have html page:


I need to get data (td tag) by choosing title (th tag).
I'm trying:

page.find(:xpath, "//body/td[count(//body/th[contains(.,'a')]/preceding-sibling::th)-1]")

and I'm expecting to get "1" as returning value, but it returns "8".
It seems to me, that "count" doesn't work right. So, what am I doing wrong?

Answer Source

The logic should've counted preceding-sibling th plus 1, to find the correct position index of the th :


xpathtester demo

output :