Capybara: Unable to find css

前端 未结 6 1214
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 18:16

I am using capybara to click a checkbox, however it can\'t seem to find it no matter what I do. I am able to correctly find both the span and the label inside the span, but

相关标签:
6条回答
  • 2021-01-03 18:30

    The problem is that the page is not being rendered because you're taken to some other page. To solve this, you don't need to change you're Capybara code. You will likely have to make some changes in your controller code.

    I got this idea since you brought up in one of the comments that you are taken to your domain's internal server when doing save_and_open_page instead. Please provide me with the details of what you see in the internal server. Are there any error messages you see there? Also, please provide me with your code for the controller action of that view you want to check a checkbox on and any other code you defined that's called in that controller action.

    0 讨论(0)
  • 2021-01-03 18:33

    Try to add :visible option set to false.

    find('#agreement', visible: false).click
    

    By default Capybara finds only visible elements. It seems that underlying driver identified this input as invisible so it hasn't been found by Capybara.

    :visible option is also supported by most of other Capybara methods (like check, has_css?, have_selector, etc.)

    0 讨论(0)
  • 2021-01-03 18:34

    I had the exact issue yesterday. Capybara was automatically ignoring the input due to it being invisible. I solved it with the following:

    find('#agreement', :visible => false).click
    

    You can also add the following to env.rb to enable Capybara to interact with all hidden elements:

    Capybara.ignore_hidden_elements = false
    
    0 讨论(0)
  • 2021-01-03 18:35

    I have the same problem ... I tried the following it works fine ...

    find('#tos', visible: false).set(true)
    
    0 讨论(0)
  • 2021-01-03 18:47

    Hard to tell without seeing the whole HTML page. Here are some possible problems:

    1. You may be looking for the checkbox before the page is fully loaded? Make sure you have enough wait time before calling find(#agreement)
    2. You may have multiple tags with the same id="agreement". Make sure you only have one.
    3. Make sure the page is valid HTML.
    4. Make sure the checkbox is visible, and enabled, and agreeValidate() is working properly.
    0 讨论(0)
  • 2021-01-03 18:50

    Try this page.execute_script("$('#agreement').attr('checked', true)"). To make this work you should tag your examples with js: true

    0 讨论(0)
提交回复
热议问题