2014-01-27 26 views
1

我的页面在顶部(一个标题和另一个部分)包含两个div,这些div在页面的其余部分可以滚动时是固定的。我需要将鼠标悬停在链接元素上,然后单击悬停在链接上时出现的按钮。由于我使用的是页面对象gem,我尝试使用scroll_into_view。然而,这个链接仍然落后于固定div。这可以防止显示按钮。有什么办法可以强迫它进入视野?页面可滚动区域顶部和底部的链接正常工作,但页面中间的项目出现问题,因为它们在滚动时出现在固定div后面。我正在使用ruby + watir-webdriver和页面对象gem。将固定元素背后的元素带入页面对象gem

很抱歉,我无法发布网站。

我的代码看起来是这样的:

class MyPage 
    div(:items, :class => 'product_items') 

    def index_for(product) 
    index = items_elements.find_index{|x| x.h4_element.text == product} 
    index 
    end 

    def add_product(product) 
    index = index_for(product) 
    product = items_elements[index.to_i] 
    product.link_element(:class => 'product_more_info').when_present.scroll_into_view 
    product.link_element(:class => 'product_more_info').hover 
    product.button_element(:class => 'product_info_button').when_present.click 
    end 
end 

在页面中间的环节仍然是固定的div后面。当它悬停时,它实际上会触发标题中的导航下拉菜单,因为该链接直接位于其后面。似乎适用于70%的链接。目前中间的30%是问题。

+1

是有,你可以连接到任何方式有问题的网站?我明白如果你不能。 – snowe

+0

也是,发布你的代码将有极大的帮助。 – snowe

+0

嗯。所以如果我正确地理解你,你有一个你需要悬停的链接。当你将鼠标悬停在上面时,会出现一些按钮。但链接是在一些divs后面?你可以悬停在这个元素上,并看到按钮,而不使用watir? – snowe

回答

0

我想我已经在下面的页面中转载了您的问题。当要悬停的div元素滚动到视图中时,它会显示在菜单下方。悬停不会导致onmouseover触发。

<html> 
    <body> 
    <div style="position:fixed; left:0; top:0; z-index=99999; border:2px solid red; width:100%">menu</div> 
    <div class="spacer" style="height:2000px"></div> 
    <div id="hoverable" onmouseover="document.getElementById('target').style.display = '';">to hover</div> 
    <button id="target" style="display:none;">the button</button> 
    <div class="spacer" style="height:2000px"></div> 
    </body> 
</html> 

有效的解决方案之一(至少在本示例页面中)是尝试悬停在元素上。如果该按钮没有出现,则假定菜单已被阻止,向后滚动页面并再次尝试。假设上面的页面,这可能与页面对象来完成:

class MyPage 
    include PageObject 

    div(:hoverable, :id => "hoverable") 
    button(:target, :id => "target") 

    def hover() 
    # Try to hover over the element 
    hoverable_element.when_present.hover 

    # If the button element does not appear, the menu must be in the way. 
    # Scroll back up 100 px so that the div appears below the menu and try again. 
    unless target_element.visible? 
     execute_script('window.scrollBy(0,-100);') 
     hoverable_element.hover 
    end 

    # Check that the button appears as expected 
    p target_element.visible? 
    #=> true 
    end 
end 

将同样的想法,你的页面对象,该add_product方法将成为:

def add_product(product) 
    index = index_for(product) 
    product = items_elements[index.to_i] 
    product.link_element(:class => 'product_more_info').hover 
    unless button_element(:class => 'product_info_button').visible? 
    execute_script('window.scrollBy(0,-100);') 
    product.link_element(:class => 'product_more_info').hover 
    end 
    product.button_element(:class => 'product_info_button').click 
end 
+0

谢谢贾斯汀。我将不得不尝试一下。仍尝试获得发布页面或屏幕截图的权限。 –