Is it possible to interact with hidden elements with capybara?

前端 未结 7 936
孤街浪徒
孤街浪徒 2021-02-06 21:08

I have a file field that has opacity: 0 and is overlaping a fake button. Its a common css technic to fake a sort of \"Upload button\" that displays consistently acr

相关标签:
7条回答
  • 2021-02-06 21:35

    You can interact with hidden elements using the visible: false property in Capybara.

    If you want to click on hidden element use:

    find("#photos", visible: false).click
    

    Don't use click_button('#photo') directly

    0 讨论(0)
  • 2021-02-06 21:45

    The author of Capybara recommends setting Capybara.ignore_hidden_elements immediately prior to needing to see the invisible element, and resetting it afterwards:

    Capybara.ignore_hidden_elements = false
    click_button 'my invisible button'
    Capybara.ignore_hidden_elements = true
    
    0 讨论(0)
  • 2021-02-06 21:45

    I've done it this way with elements that has the CSS style display:none; set:

    page.execute_script("$('.all-hidden-elements').show();");
    all('.all-hidden-elements').first.click
    
    0 讨论(0)
  • 2021-02-06 21:48

    Miquel, thanks for workaraund.

    I have similar issue for interacting with hidden file input on C# binding for Selenium Webdriver 2.35 and Firefox 24. To make file selection working did similar trick:

    ((IJavaScriptExecutor)webdriver).ExecuteScript("$('#fileUploadInput').css({opacity: 1, transform: 'none'});");
    
    IWebElement e = webdriver.FindElement(By.CssSelector("input#fileUploadInput")));
    
    e.SendKeys("c:\\temp\\inputfile.txt");
    
    0 讨论(0)
  • 2021-02-06 21:49

    I ended up resolving it a different route.

    execute_script() was giving me a hard time (it would freeze test execution on FireFox), so this is what I did:

    I already had an appropriate javascript file. I appended the following

    <% if ENV["RAILS_ENV"] == "test" %>
      $('#photos').show()
    <% end %>
    

    I also had to append .erb to my javascript file for proper Rails asset handling.

    And in my test file, I was already setting ENV["RAILS_ENV"] = "test"

    This way I could just dumb down the UI for test, and yet maintain the look and feel for production.

    0 讨论(0)
  • 2021-02-06 21:55

    If the hidden element is nested in a visible parent element (e.g. a hidden input inside a visible label), you can click on the parent instead. If you still want to find the input by ID, you can traverse to the parent like so:

    find('#hidden_input').find(:xpath, '..').click
    
    0 讨论(0)
提交回复
热议问题