使用' watir-webdriver'模拟嵌套链接。

时间:2017-02-05 10:52:50

标签: ruby automated-tests watir watir-webdriver

我正在使用以下HTML来模拟链接点击' watir-webdriver'它嵌套在

<!DOCTYPE html>
    <html>
        <head>
           <title>Jothidhal</title>
           <meta charset = "utf-8">
           <meta name = "viewport" content = "width = device-width ,  initial-scale = 1>
           <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.min.css>
       </head>
       <body>
           <div class = "container">
               <div class = "row">
                   <div class = "col-md-12 well" >col-12</div>//Am trying to use "well", but i couldn't get it.i guess the probelm is in linking part, even i couldn't use any other features of CSS, am a beginner and couldn't get through this. Thanks in advance to anyone who helps in with this.. 
               </div>
           </div>
           <script type = "text/javascript" src = "js/bootstrap.min.js"></script>
           <script type = "text/javascript" src = "js/jquery.js"></script>
       </body>
    </html>  

我的脚本非常基本:

<li>
<div class="drop-head">
<ul>
    <li>
        <a href="#hotel" aria-label="go to Hotels" title="Hotels">Hotels</a>
    </li>
.....
</ul>
</div>

在控制台中,我发现以下错误:

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

对此有何帮助?

1 个答案:

答案 0 :(得分:1)

问题可能是元素路径的过度规范。当你这样做时:

menu = browser.div(:class => "drop-head").li.a(:href => "#hotel").click

它实际上找到第一个匹配的“drop-head”div,并在其中找到第一个li元素。鉴于每个链接可能都在其自己的li元素中,您将只能找到第一个链接。例如:

<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>

寻找第二个链接“#restaurants”:

browser.div(:class => "drop-head").li.a(:href => "#restaurants")

实际上是在第一个li元素中寻找链接:

<li>
 <a href="#hotel" aria-label="go to Hotels" title="Hotels">Hotels</a>
</li>

解决方案是仅在实际需要区分元素时指定元素的路径。假设只有一个链接href,你可以这样做:

browser.a(:href => "#hotel").click