Selenium2 WebDriver Ruby => how click on a hidden link

后端 未结 3 1592
北恋
北恋 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: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
    

提交回复
热议问题