Sukhminder Singh
Sukhminder Singh

Reputation: 157

Cucumber Capybara - Find an element based on attribute's content

My requirement is to check if some particular content (i.e. Id=4540484 in the below example) exists in the onclick attribute on an anchor tag on a page. Can someone help me out please?

<a title="Acrobat PDF File" href="#" onclick="openPop('/example/secure/Screen.aspx?Id=4540484&amp;certdocId=2513235)"><img src="New.gif" align="middle" border="0"></a>

Upvotes: 0

Views: 940

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

If you want to find an element based on the onclick attribute, you will need to use a css (or xpath) selector.

In your case, you want the css-selector:

a[onclick*="Id=4540484"]

This says to find a link element where the onlclick attribute contains the text "Id=4540484".

You can then use the has_css? method to check if that link is on the page:

#Using an ID that exists will return true
page.has_css?('a[onclick*="Id=4540484"]')
#=> true

#Using an ID that does not exist will return false
page.has_css?('a[onclick*="Id=9999999"]')
#=> false

Upvotes: 1

Related Questions