无法使用硒黄瓜通过水豚点击评级图像

Unable to click the rating image via capybara using selenium-cucumber

这是下面的 html 代码。当鼠标悬停在星图上时,图源变为star-on.png。如果您看到下面的代码,则表示我给了两星评级。

<div id="user_rating" class="rating" style="cursor: pointer; width: 100px;">
<img src="/assets/jquery.raty/star-on.png" alt="1" title="">&nbsp;
<img src="/assets/jquery.raty/star-on.png" alt="2" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="3" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="4" title="">&nbsp;
<img src="/assets/jquery.raty/star-off.png" alt="5" title="">
<input type="hidden" name="score"></div>

我使用 img src、all 和 values 在黄瓜中尝试了以下代码,但我无法点击评级图像

img_src 传递为“/assets/jquery.raty/star-off.png”

Then /^I click on the image with src "([^\"]*)"$/ do |img_src|
   find("img[src=\'#{img_src}\']", match: :prefer_exact).click
end

alt_text 传递为“3”

Then /^I click on the image with alt "([^\"]*)"$/ do |alt_text|
  page.execute_script %Q[jQuery("img[alt=\'{alt_text}\']").first().mouseenter();]
  page.execute_script %Q[jQuery("img[alt=\'#{alt_text}\']").first().click();]
end

选择器 = 传递正确的 xpath

And /^I click by xpath "([^\"]*)"$/ do |selector|
  find(:xpath, selector, visible: true, match: :first).click
end

谁能告诉我一个正确的 selenium 命令来点击评级

尽量不要在前两个解决方案中转义单引号:\' => '。有用吗?

第二个解决方案中的 javascript 应该更改:

page.execute_script %Q[jQuery("img[alt=\'#{alt_text}\']").first().trigger('click');]

要点击特定的星星,您需要执行以下操作

find("#user_rating img[alt='2']").hover.click # select star 2

所以在黄瓜步骤中,类似于

Then /^I rate ([^ ]+) with (\d) stars$/ do |id, stars|
  find("##{id} img[alt='#{stars}']").hover.click
end

并被称为

Then I rate user_rating with 4 stars