holyonline
holyonline

Reputation: 60

How to locate element? Illegal selector error (Capybara)

I'm trying to use a page.find to locate a element using a text variable (both elements don't complete entirely), but when I tried to use the locator, I got the error: invalid selector: An invalid or illegal selector was specified .

I don't know if the method is wrong or the the problem is the html element.

it 'test1' do visit 'https://www.americanas.com.br/'

    $name = box for all the boys i've loved
    $author = Jenny Han

    fill_in 'h_search-input',   with: $isbn
    click_button 'h_search-btn'

    americanas = page.find($name).text      # <- here is the error
    americanas.click

    expect(find('table tbody tr')).to have_content $author
end

HTML elements:

<h1 class="TitleUI-sc-1m3ayw0-16 cvOkHP TitleH1-c6mv26-0 VyqrE" icon="[object Object]">Box Book - For All The Boys I've Loved</h1>

and

<tr class="table-content__Tr-sc-6kkk7q-3 jFIPzA"><td class="table-content__Td-sc-6kkk7q-5 bPPRTT"><span class="text__TextUI-sc-1hrwx40-0 cKQvNJ">Author</span></td><td class="table-content__Td-sc-6kkk7q-5 bPPRTT"><span class="text__TextUI-sc-1hrwx40-0 cKQvNJ">Jenny Han</span></td></tr>

What I got:

 1) test test1
 Failure/Error: americanas = page.find($name).text

 Selenium::WebDriver::Error::InvalidSelectorError:
   invalid selector: An invalid or illegal selector was specified
     (Session info: chrome=74.0.3729.169)
     (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)
 # ./spec/teste_spec.rb:30:in `block (2 levels) in <top (required)>'

Thanks for your time!

Upvotes: 0

Views: 1097

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

find takes a selector type (defaults to :css), a locator, and then options - and returns a single matching element. In your code

americanas = page.find($name).text
americanas.click

You are not passing a selector type to find so it's assumed to be :css, but then you're passing a random string of text (I assume it's a string but it has no quotes around it in your code though?) which isn't a valid CSS selector - hence the error. After that you're calling text on the result of find which (if your find was correct) would get the text from the element (as a string) and then you call click on the string which doesn't make any sense since click is only supported on elements. I think what you're trying to do is

page.find('h1', text: $name).click

but even that is going to fail because you have $name = "box for all the boys i've loved" but the contents of the h1 are "Box Book - For All The Boys I've Loved" which doesn't match in contents or case. You could use a case insensitive regexp by doing something like

page.find('h1', text: /for all the boys i've loved/i).click 

but it all depends on what you're actually trying to do.

Then, assuming your table has more than one table row your expectation would probably need to be something like

expect(page).to have_selector('tr', text: $author)

Note: Based on the questions you've been asking it really seems like you could benefit from reading the Capybara docs and a few ruby tutorials.

Upvotes: 1

Related Questions