Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to find css #31

Open
nekapoor opened this issue Nov 11, 2014 · 22 comments
Open

unable to find css #31

nekapoor opened this issue Nov 11, 2014 · 22 comments

Comments

@nekapoor
Copy link

Hello!

I'm running into an error when running this in rspec. I get this error:

Capybara::ElementNotFound:
Unable to find css ".select2-drop li" with text "Test"

When I run rspec, my browser starts up and the dropdown does actually open up and I see the "Test" item there, but then the dropdown just closes up without selecting the "Test" item. And then I get this "unable to find css" error.

Any ideas on what's going on?

@thewatts
Copy link

thewatts commented Dec 7, 2014

I'm having a similar issue 👍

@kiddrew
Copy link

kiddrew commented Jan 28, 2015

Me too:

Unable to find css ".select2-choice"

I'm using select2 v 3.5.2, and I don't see a select2-choice class anywhere. Maybe the gem is intended for another version?

@divineforest
Copy link
Collaborator

Try using gem from github, not from rubygems

@kiddrew
Copy link

kiddrew commented Jan 29, 2015

Did that. Now it's throwing a different "unable to find css" error.

Unable to find css ".select2-with-searchbox input.select2-input"

@MelnikVasya
Copy link

Gem from github

Capybara::ElementNotFound:
       Unable to find css ".select2-choices"

@bertBruynooghe
Copy link

Can any of you post the code you're trying to execute?

@lcguida
Copy link

lcguida commented Oct 22, 2015

I'm using version 4.0.0 and receiving the same message.

(byebug) select2 site.name, from: "Site"
*** Capybara::ElementNotFound Exception: Unable to find css ".select2-choices"

@josegm
Copy link

josegm commented Nov 15, 2015

Similar message for me:

Capybara::ElementNotFound:
       Unable to find css ".select2-container"

The select2-container class is there if I inspect the element.

@jngarcia-zz
Copy link

Me too:

Capybara::ElementNotFound:
  Unable to find css ".select2-container"

@lvanhung
Copy link

I got same issue :(

Unable to find css ".select2-container"

@rkotov93
Copy link

rkotov93 commented Jan 4, 2016

Hi!
I had the same issue with

Capybara::ElementNotFound:
  Unable to find css ".select2-container"

Try to use github version of gem. It was helpful for me.

@rkotov93
Copy link

rkotov93 commented Jan 4, 2016

But I still have

Capybara::ElementNotFound:
  Unable to find css ".select2-choices"

To solve it, I specified xpath to select2 container, because gem looks exactly for it as it turned out:

xpath: '//div[contains(@class, "select2-container")]'

@lvanhung
Copy link

lvanhung commented Jan 5, 2016

@rkotov93
Thanks for your sharing.
How about if the page has multiple select2 ? I guess your solution will not work.
I found a solution in stackoverflow. it works with selenium driver

   def select2(value, attrs)
        s2c = first("#s2id_#{attrs}")
        (s2c.first(".select2-choice") || s2c.find(".select2-choices")).click
        find(:xpath, "//body").all("input.select2-input")[-1].set(value)
        page.execute_script(%|$("input.select2-input:visible").keyup();|)
        drop_container = ".select2-results"
        find(:xpath, "//body").all("#{drop_container} li", text: value)[-1].click
    end

@rkotov93
Copy link

rkotov93 commented Jan 5, 2016

@lvanhung
Yes, you're absolutely right. Do you need to choose the same option for each select2 on the page?
I think you can try something like this:

page.all(:xpath, '//*[contains(@class, "select2-container")]').each do |s2c|
  select2(value, xpath: s2c.path)
end

Warning
This solutions should work with Selenium, but doesn't work with Poltergeist. Please, give a feedback if try.

@lvanhung
Copy link

lvanhung commented Jan 5, 2016

@rkotov93
this work for both poltergeist & Selenium
for raketest driver I use the following code

select('Option', :from => 'Select Box')

@NySiya
Copy link

NySiya commented Mar 16, 2016

I have got this error, too.
Capybara::ElementNotFound: Unable to find css ".select2-container"

@jlgasparrini
Copy link

Hi @rkotov93, sorry to bother you. Your solution doesn't work for me with capybara-webkit.

I have got this error too:

     Capybara::ElementNotFound:
       Unable to find css ".select2-choices"

Can you help me please?

@rkotov93
Copy link

rkotov93 commented May 5, 2016

@jlgasparrini Can you show your code?

@jlgasparrini
Copy link

Yes, sure @rkotov93

# Gemfile.rb
...
gem 'capybara'
gem 'capybara-webkit'
gem 'capybara-select2'
...
# report_school_type_spec.rb

feature 'report school type' do
  before(:each) do
    @prof = FactoryGirl.create :staff
    @note_type = FactoryGirl.create :note_type, institution: @prof.institution

    login_as @prof.user, scope: :user
  end

  scenario 'create with one report school column', js: true do
    visit new_staff_report_school_type_path

    fill_in ReportSchoolType.human_attribute_name(:name), with: 'Report school 2'
    find_button('Add new column').click
    fill_in 'Amount of notes', with: '3'

    #select2 @note_type.name, xpath: "//div[contains(@class, 'select2-container')]", search: true

    page.all(:xpath, '//*[contains(@class, "select2-container")]').each do |s2c|
      select2(@note_type.name, xpath: s2c.path)
    end

    click_button I18n.t 'staff.report_school_types.new.new'
    msg = I18n.t 'staff.report_school_types.create.success'
    expect(page).to have_content msg
  end
end

And when I run the test suite bin/rails spec
I have got:

     Failure/Error: select2(@note_type.name, xpath: s2c.path)
     Capybara::ElementNotFound:
       Unable to find css ".select2-choices"

Thanks @rkotov93 ^^

@rkotov93
Copy link

rkotov93 commented May 5, 2016

@jlgasparrini It looks like you really haven't any choises on the page. Can you show your view?
Also, try to make html screenshot before

page.all(:xpath, '//*[contains(@class, "select2-container")]').each do |s2c|
  select2(@note_type.name, xpath: s2c.path)
end

and check the code for choices.
You can use capybara-screenshot gem for it.

@jlgasparrini
Copy link

jlgasparrini commented May 5, 2016

Exactly @rkotov93, you're right! I search in my html and the selector .select2-choices didn't exist. The screenshot show me that Select2 was opened but nothing inside.

I think the problem is because Select2 options not yet exist, these are obtained from my server.

<!-- _my_partial.html.erb -->
<div class="col-xs-12 col-sm-6 form-group">
  <%= f.label :note_type_id %>
  <%= f.select :note_type_id, nil, {}, { class: 'form-control' } %>
</div>
# report_school_types.coffee.erb
ReportParameter.note_types = do ->
  init: ->
    select2_options = do ->
      minimumInputLength: 2
      theme: 'bootstrap'
      ajax:
        url: "<%= url_helpers.staff_notes_types_path(format: :json) %>"
        dataType: 'json'
        data: (params) ->
          selected_note_types = new Array()
          $('#report-parameters select').each (i, note_type) ->
            selected_note_types.push note_type.name if note_type.name
          return {
            select_q: params.term
            selected: selected_note_types
          }
        processResults: (data, params) ->
          processed = []
          $.each data, (index, item) ->
            processed.push
              id: item.id
              text: item.name + ' (' + item.abbreviation + ')'
          return results: processed
    $('#report-parameters')
      .on 'cocoon:before-insert', (e, to_be_added) ->
        to_be_added.fadeIn(250)
      .on 'cocoon:after-insert', (e, added) ->
        $('select:first', added).select2(select2_options).select2('open')
      .on 'cocoon:before-remove', (e, to_be_removed) ->
        $(this).data('remove-timeout', 200)
        to_be_removed.fadeOut(150)
    $('#report-parameters')
      .on 'cocoon:before-insert', (e, to_be_added) ->
        to_be_added.fadeIn(250)
      .on 'cocoon:after-insert', (e, added) ->
        $('select:last', added).select2(select2_options)
      .on 'cocoon:before-remove', (e, to_be_removed) ->
        $(this).data('remove-timeout', 200)
        to_be_removed.fadeOut(150)

@rkotov93
Copy link

rkotov93 commented May 5, 2016

@jlgasparrini Then you have to initialize select2 in the ajax callback ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests