使用selenium执行点击操作时遇到Mozilla问题

时间:2018-03-16 11:09:12

标签: java selenium selenium-webdriver webdriver

以下是网页的HTML:

<div class="col-sm-6 col-md-4 col-lg-3">                   
<section class="product-cell">
<div class="product-cell-inner ribbon-item ">
<div class="product-thumb">
<a href="/bath/product/P18437-BN" target="_self">
<img class="img-responsive center-block" src="https://media.peerlessfaucet.com/elvis/OnWhite/sm/P18437-BN-B1.png">
 </a>
</div>
<div class="product-info">
<div class="product-heading" style="height: 104px;">
 <button class="product-image-button" data-toggle="modal" data-target="#product-modal-3489cb3e-45ff-4e14-9d03-308c78090ed6" data-image="https://media.peerlessfaucet.com/elvis/OnWhite/md/P18437-BN-B1.png" data-details="/bath/product/P18437-BN">View Larger</button>
 <h5><a href="/bath/product/P18437-BN">Tub and Shower Complete</a></h5>
 </div>
 <div class="product-info-inner">
 <div class="product-info-inner-border" style="height: 61px;">
 <dl class="dl-horizontal">
<dt>Model:</dt>
<dd>P18437-BN</dd>
<dt>List price as shown:<sup>1</sup></dt>    
<dd class="price">$198.50</dd>                    
</dl>
 </div>
<a class="more-less">
<span class="less">Less Information <b><span class="glyphicon glyphicon-triangle-top"></span></b></span>
<span class="more">More Information <b><span class="glyphicon glyphicon-triangle-bottom"></span></b></span>
</a>
 <div class="extended-content hidden">   
<h6 class="available-finishes">Available Finishes:</h6>
<ul class="finishes">
 <li><a href="/bath/product/P18437-BN" class="replaced finish-link bn selected">
<span class="facet-thumb facetfinish" style="background: url(/files/live/sites/peerless/files/sprites/sprite-filter-finishes.jpg) no-repeat -296px 0px;
width: 37px;
height: 37px;"></span>
</a></li>  
</ul>                    
 <p class="specs-link"><a href="/bath/product/P18437-BN">View Specs, Dimensions, Installation Instructions, and Parts Diagrams</a></p>
</div>
 </div>
 /div>
<div class="add-to-compare">
<div class="checkbox">
  <label><input type="checkbox" value="P18437-BN" name="modelNumber">Add to Compare</label>
</div>
</div>
</div>
</section> 

我写了下面的代码来点击第一个元素:

List<WebElement> elements = driver.findElements(By.className("product-thumb"));
System.out.println("The numbers of elements in page--->"+elements.size());
System.out.println("first element name is---->"+(elements.get(0).getText()));
elements.get(0).click();

我想打开列表中的第一个元素。相同的代码在chrome中工作,但在Mozilla中执行时,它显示元素已被点击但实际上它并不是狡猾的。所以我的脚本失败了。你能帮我一下吗?

1 个答案:

答案 0 :(得分:0)

如果您打算单击标识为<a href="/bath/product/P18437-BN" target="_self">的元素,则需要考虑以下因素:

  • 根据 HTML

    <div class="product-thumb">
        <a href="/bath/product/P18437-BN" target="_self">
            <img class="img-responsive center-block" src="https://media.peerlessfaucet.com/elvis/OnWhite/sm/P18437-BN-B1.png">
        </a>
    </div>
    
  • By.className("product-thumb")仅会识别以上单亲<div>元素。
  • 因此,尺寸总是 1
  • 节点本身没有文字,因此getText()将返回 null
  • 要点击<a>标记,您可以按如下方式简化代码块:

    driver.findElement(By.xpath("//div[@class='product-thumb']/a")).click();