[英]Simulating a nested link click with 'watir-webdriver'
I'm working with the following HTML to simulate link click with 'watir-webdriver' which is nested inside我正在使用以下 HTML 来模拟带有嵌套在内部的“watir-webdriver”的链接点击
<li>
<div class="drop-head">
<ul>
<li>
<a href="#hotel" aria-label="go to Hotels" title="Hotels">Hotels</a>
</li>
.....
</ul>
</div>
My script is very basic:我的脚本非常基本:
require 'watir-webdriver'
browser = Watir::Browser.new :firefox
browser.goto "https://www.acme,com/reserve"
wait = Selenium::WebDriver::Wait.new(:timeout => 5)
menu = browser.div(:class => "drop-head").li.a(:href => "#hotel").click
In the console I'm seeing the following error :在控制台中,我看到以下错误:
C:/apps/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.9/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found': unable to locate element, using {:href=>"#hotel", :tag_name=>"a"} (Watir::Exception::UnknownObjectException)
from C:/apps/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.9/lib/watir-webdriver/elements/element.rb:508:in `assert_exists'
from C:/apps/Ruby23/lib/ruby/gems/2.3.0/gems/watir-webdriver-0.9.9/lib/watir-webdriver/elements/element.rb:114:in `click'
from script4.rb:7:in `<main>'
Any help on this?这有什么帮助吗?
The problem is likely an over specification of the element's path.问题可能是元素路径的过度规范。 When you do:
当你这样做时:
menu = browser.div(:class => "drop-head").li.a(:href => "#hotel").click
It actually finds the first matching "drop-head" div
and within that the first li
element.它实际上会找到第一个匹配的“drop-head”
div
以及第一个li
元素。 Given that each link is likely in its own li
element, you will only ever find the first link.鉴于每个链接都可能在其自己的
li
元素中,您只能找到第一个链接。 For example in:例如在:
<div class="drop-head">
<ul>
<li>
<a href="#hotel" aria-label="go to Hotels" title="Hotels">Hotels</a>
</li>
<li>
<a href="#restaurants" aria-label="go to Restaurants" title="Restaurants">Restaurants</a>
</li>
</ul>
</div>
Looking for the second link, "#restaurants":寻找第二个链接“#restaurants”:
browser.div(:class => "drop-head").li.a(:href => "#restaurants")
Will actually be looking for the link in the first li
element:实际上会在第一个
li
元素中寻找链接:
<li>
<a href="#hotel" aria-label="go to Hotels" title="Hotels">Hotels</a>
</li>
The solution is to only specify the element's path when actually necessary to differentiate the element.解决方案是仅在实际需要区分元素时才指定元素的路径。 Assuming there is only one link with that
href
, you can probably just do:假设只有一个链接与该
href
,你可以这样做:
browser.a(:href => "#hotel").click
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.