Capybara: Clicking elements that are not links or buttons.

Created on 27 Nov 2013  Â·  5Comments  Â·  Source: teamcapybara/capybara

I have a situation where I must click a span elements, which makes parts of the DOM vissible (that I must verify). A javascript click eventhandler makes the part of the DOM vissble. But when using capybaras click_on it seems that it only searches through a and button elements. So triggering this javascript click eventhandler seems impossible.

Most helpful comment

Thanks a lot. that was a way around it... I use

page.find(:xpath,"//*[text()='#{click_text}']").click

where click_text is the human readable text.

All 5 comments

Have you tried using find, e.g.

find('#id_of_element').click

Thanks a lot. that was a way around it... I use

page.find(:xpath,"//*[text()='#{click_text}']").click

where click_text is the human readable text.

You should consider using normalize-space() rather than text() - this is what Capybara (XPath) uses internally for finding nodes by text - as the actual text of your html nodes often will contain trailing and following whitespace, and also multiple whitespace between words, which is normalized by the browser for display.

Thanks for the tip.

2013/11/28 Adrian Longley [email protected]

You should consider using normalize-space() rather than text() - this is
what Capybara (XPath) uses internally for finding nodes by text - as the
actual text of your html nodes often will contain trailing and following
whitespace, and also multiple whitespace between words, which is normalized
by the browser for display.

—
Reply to this email directly or view it on GitHubhttps://github.com/jnicklas/capybara/issues/1200#issuecomment-29449125
.

page.find_all(:xpath, "//*[normalize-space(text())='#{link_text}']").first.click

Was this page helpful?
0 / 5 - 0 ratings