watir - 使用悬停单击隐藏的元素

时间:2014-01-16 19:15:05

标签: ruby selenium watir

我正在点击隐藏在其顶部的隐藏元素。我试过点击两次,但这不起作用。我没有得到任何错误,但它只是不起作用。有人可以帮忙吗?

错误:

# Running tests:

#<Watir::LI:0x007fb213b01ec8>
(0)
F

Finished tests in 11.174010s, 0.0895 tests/s, 0.1790 assertions/s.

1) Failure:
Tests#test_debug     [...Cucumber/features/debug.    rb:27]:
Failed assertion, no message given.

1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
[Finished in 12.3s with exit code 1]

违规的HTML:

<li title="Optic White">
<img id="swatch456180#7_0" class="colorSwatch"style="background:url('http://slimages.macys.com/is/image/MY/&amp;$b=MCY/swatches/&amp;layer=0&amp;size=247,19&amp;src=is{$b$0/optimized/351640_fpx.tif}&amp;cropN=0,0,13,1&amp;anchor=0,0&amp;layer=1&amp;size=19,19&amp;src=is{$b$9/optimized/1200269_fpx.tif}&amp;anchor=0,0&amp;posN=0.077,0&amp;layer=2&amp;size=19,19&amp;src=is{$b$0/optimized/1200270_fpx.tif}&amp;anchor=0,0&amp;posN=0.154,0&amp;layer=3&amp;size=19,19&amp;src=is{$b$4/optimized/1611224_fpx.tif}&amp;anchor=0,0&amp;posN=0.231,0&amp;layer=4&amp;size=19,19&amp;src=is{$b$2/optimized/1227542_fpx.tif}&amp;anchor=0,0&amp;posN=0.308,0&amp;layer=5&amp;size=19,19&amp;src=is{$b$3/optimized/740793_fpx.tif}&amp;anchor=0,0&amp;posN=0.385,0&amp;layer=6&amp;size=19,19&amp;src=is{$b$2/optimized/740792_fpx.tif}&amp;anchor=0,0&amp;posN=0.462,0&amp;layer=7&amp;size=19,19&amp;src=is{$b$0/optimized/1016310_fpx.tif}&amp;anchor=0,0&amp;posN=0.538,0&amp;layer=8&amp;size=19,19&amp;src=is{$b$9/optimized/1014329_fpx.tif}&amp;anchor=0,0&amp;posN=0.615,0&amp;layer=9&amp;size=19,19&amp;src=is{$b$4/optimized/740794_fpx.tif}&amp;anchor=0,0&amp;posN=0.692,0&amp;layer=10&amp;size=19,19&amp;src=is{$b$8/optimized/740798_fpx.tif}&amp;anchor=0,0&amp;posN=0.769,0&amp;layer=11&amp;size=19,19&amp;src=is{$b$7/optimized/1014327_fpx.tif}&amp;anchor=0,0&amp;posN=0.846,0&amp;layer=12&amp;size=19,19&amp;src=is{$b$1/optimized/1200271_fpx.tif}&amp;anchor=0,0&amp;posN=0.923,0&amp;layer=13&amp;op_sharpen=1&amp;fmt=jpeg&amp;qlt=90,0&amp;hei=19') 133px 0 transparent;" src="http://assets.macys.com/navapp/img/spacer.gif" title="Optic White" alt="Optic White">
<input type="hidden" id="swatch456180#7_0_imgurl"value="2/optimized/740792_fpx.tif">
</li>
<img id="swatch456180#7_0" class="colorSwatch" style="background:url('http://slimages.macys.com/is/image/MCY/?&amp;$b=MCY/swatches/&amp;layer=0&amp;size=247,19&amp;src=is{$b$0/optimized/351640_fpx.tif}&amp;cropN=0,0,13,1&amp;anchor=0,0&amp;layer=1&amp;size=19,19&amp;src=is{$b$9/optimized/1200269_fpx.tif}&amp;anchor=0,0&amp;posN=0.077,0&amp;layer=2&amp;size=19,19&amp;src=is{$b$0/optimized/1200270_fpx.tif}&amp;anchor=0,0&amp;posN=0.154,0&amp;layer=3&amp;size=19,19&amp;src=is{$b$4/optimized/1611224_fpx.tif}&amp;anchor=0,0&amp;posN=0.231,0&amp;layer=4&amp;size=19,19&amp;src=is{$b$2/optimized/1227542_fpx.tif}&amp;anchor=0,0&amp;posN=0.308,0&amp;layer=5&amp;size=19,19&amp;src=is{$b$3/optimized/740793_fpx.tif}&amp;anchor=0,0&amp;posN=0.385,0&amp;layer=6&amp;size=19,19&amp;src=is{$b$2/optimized/740792_fpx.tif}&amp;anchor=0,0&amp;posN=0.462,0&amp;layer=7&amp;size=19,19&amp;src=is{$b$0/optimized/1016310_fpx.tif}&amp;anchor=0,0&amp;posN=0.538,0&amp;layer=8&amp;size=19,19&amp;src=is{$b$9/optimized/1014329_fpx.tif}&amp;anchor=0,0&amp;posN=0.615,0&amp;layer=9&amp;size=19,19&amp;src=is{$b$4/optimized/740794_fpx.tif}&amp;anchor=0,0&amp;posN=0.692,0&amp;layer=10&amp;size=19,19&amp;src=is{$b$8/optimized/740798_fpx.tif}&amp;anchor=0,0&amp;posN=0.769,0&amp;layer=11&amp;size=19,19&amp;src=is{$b$7/optimized/1014327_fpx.tif}&amp;anchor=0,0&amp;posN=0.846,0&amp;layer=12&amp;size=19,19&amp;src=is{$b$1/optimized/1200271_fpx.tif}&amp;anchor=0,0&amp;posN=0.923,0&amp;layer=13&amp;op_sharpen=1&amp;fmt=jpeg&amp;qlt=90,0&amp;hei=19') 133px 0 transparent;" src="http://assets.macys.com/navapp/img/spacer.gif" title="Optic White" alt="Optic White">

测试:

def test_debug
    before_items = @browser.span(:id => "itemCount")
    assert before_items.text == "(0)"
    @browser.goto "http://www1.macys.com/shop/product/hotel-collection-microcotton-luxe-bath-towel-collection?ID=456180&CategoryID=16853#fn=sp%3D1%26spc%3D205%26kws%3Dtowel%26slotId%3D1"
    @browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").click
    @browser.li(:title => "Optic White").click
    bagbutton = @browser.image(:class => "addToBagButton")
    puts bagbutton
    after_items = @browser.span(:id => "itemCount")
    puts after_items.text
    assert after_items.text == "(1)" 
end

1 个答案:

答案 0 :(得分:1)

选择颜色

要选择颜色,点击需要针对图像元素而不是父列表项。

例如,而不是:

@browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").click

你需要这样做:

@browser.li(:xpath => "//*[@id=\"colorList457102\"]/li[3]").image.click

在找到颜色按钮时,您可能需要进行一些改进(参见下一节)。

点击添加按钮

您的断言也会失败,因为没有点击添加包按钮。为确保您单击用于色板颜色选择的相同添加按钮,我认为您应该在代表产品的父元素中工作。

结合2个想法,添加项目的脚本部分将是:

# The list of products, 
#  where each product is contained within a div of class memberProducts
product_list = @browser.div(:id => 'memberProductList')    

# Lets assume we want the first product (the product of interest)
product = product_list.div(:class => 'memberProducts')

# Select the colour based on title (for the product of interest)
product.li(:title => 'Optic White').image.click

# Click the add to bag button (for the product of interest)
product.image(:id => /addToBagButton/).click    

# Wait for the add to cart popup to be displayed (ie page finishes processing request)
@browser.div(:id => 'pdpMasterAddToBagPanel').wait_until_present