Rahul Rahul - 2 years ago 41
Ruby Question

Find element with special characters in attribute value

I have the following html:

<textarea class="input-validation-error form-control" cols="20"
data-val="true" data-val-regex="Invalid input"
data-val-required="Please enter valid values"
id="val" name="values" rows="9"></textarea>

I am trying to assert
for its validation:

expect(page).to have_css(

but I keep getting
invalid selector
error. I have tried escaping the special characters also wrapping the whole value in both single and double quotes. What should this be asserted?

Answer Source

The issue you're having here is that you have to escape for both the ruby string and escape for CSS. The character causing you the issues is the \ character which ends up needing to be double escaped, becoming \\\\

expect(page).to have_css('textarea[data-val-regex-pattern="([a-zA-Z][0-9]{11}[a-zA-Z0-9][^\\\\S\\\\r\\\\n]*\\\\r\\\\n)*[a-zA-Z][0-9]{11}[a-zA-Z0-9][\\\\s]*"]')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download