Selenium2 WebDriver Ruby => how click on a hidden link

后端 未结 3 1591
北恋
北恋 2021-01-06 01:59

I use Selenium 2 WebDriver on Ruby.

How it is possible click on hidden link, with css (display: none)? the link is submenu and is visible when mouse over on menu.

相关标签:
3条回答
  • 2021-01-06 02:14

    WebDriver emulates user actions, and doesn't allow clicking elements that a user wouldn't be able to click.

    So you should do what a user would do: mouse over the menu before clicking. In Ruby you could do e.g.:

    menu = driver.find_element(:id => "menu")
    submenu = driver.find_element(:id => "submenu")
    
    driver.action.move_to(menu).click(submenu).perform
    

    The ActionBuilder class (returned by Driver#action) is documented here.

    0 讨论(0)
  • 2021-01-06 02:16

    before you search the link and click it

    @driver.execute_script('jQuery(":hidden","#nav-main").show()')
    

    jQuery should be loaded

    0 讨论(0)
  • 2021-01-06 02:36

    In the past, if I have not had much luck with the ActionBuilder methods, I just use pure javascript:

    def mouse_over_and_out(element)
      @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}", element)
      yield element
      @driver.execute_script("if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseout', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseout');}", element)
    end
    
    def wait_for_visibility(element) 
      wait = Selenium::WebDriver::Wait.new(:timeout => 15)
      wait.until do
        element.visible?
      end
      element
    end
    
    mouse_over_and_out(@driver.find_element(:css => "ul#nav-main li")) do |menu|
      submenu = menu.find_element(:css => "ul li a")
      wait_for_visibility(submenu).click
    end
    
    0 讨论(0)
提交回复
热议问题