【问题标题】:Facing issue with Mozilla while performing click action using selenium使用 selenium 执行点击操作时遇到 Mozilla 的问题
【发布时间】:2018-03-16 11:09:11
【问题描述】:

以下是网页的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 中执行时,它显示元素已单击,但实际上它并没有响起。所以我的脚本失败了。你能帮我做同样的事情吗?

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver


    【解决方案1】:

    如果您打算点击标识为&lt;a href="/bath/product/P18437-BN" target="_self"&gt; 的元素,您需要考虑以下事项:

    • 根据 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") 将仅识别上面的单个父 &lt;div&gt; 元素。
    • 因此大小将始终为 1
    • 节点本身没有文本,因此getText() 将返回null
    • 要点击&lt;a&gt; 标签,您可以将代码块简化如下:

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

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多