Java 如何查找表中的值和下的行中的按钮?

Java 如何查找表中的值和下的行中的按钮?,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我想在表中找到值,然后按下与该值相连的按钮。这是我的例子,我有比较产品,我想从比较产品中删除“缺货”的产品 这是我正在工作的页面 这是我的密码: public void findAvailability(){ String searchingAvailabilityValue = "Out Of Stock"; int availabilityPosition =0; int numOfFields = availabilityFields.size();

我想在表中找到值,然后按下与该值相连的按钮。这是我的例子,我有比较产品,我想从比较产品中删除“缺货”的产品

这是我正在工作的页面

这是我的密码:

public void findAvailability(){

    String searchingAvailabilityValue = "Out Of Stock";
    int availabilityPosition =0;
    int numOfFields =   availabilityFields.size();

    for(int i=0;i<numOfFields;i++){
        String availabilityValues = availabilityFields.get(i).getText();

        if(availabilityValues.contains(searchingAvailabilityValue)){
            availabilityPosition = i+2;
            WebElement removeButton = driver.findElement(By.xpath("//*[@id='content']/table/tbody[2]/tr/td["+availabilityPosition+"]/a"));
            removeButton.click();                           
        }   

    }
以下是html:

<tbody>
  <tr>
    <td>Product
    </td>
    <td>
      <a href="http://demo.opencart.com/index.php?route=product/product&amp;product_id=48">
        <strong>iPod Classic
        </strong>
      </a>
    </td>
    <td>
      <a href="http://demo.opencart.com/index.php?route=product/product&amp;product_id=36">
        <strong>iPod Nano
        </strong>
      </a>
    </td>
    <td>
      <a href="http://demo.opencart.com/index.php?route=product/product&amp;product_id=34">
        <strong>iPod Shuffle
        </strong>
      </a>
    </td>
    <td>
      <a href="http://demo.opencart.com/index.php?route=product/product&amp;product_id=32">
        <strong>iPod Touch
        </strong>
      </a>
    </td>
  </tr>
  <tr>
    <td>Image
    </td>
    <td class="text-center">              
      <img src="http://demo.opencart.com/image/cache/catalog/demo/ipod_classic_1-90x90.jpg" alt="iPod Classic" title="iPod Classic" class="img-thumbnail">
    </td>
    <td class="text-center">              
      <img src="http://demo.opencart.com/image/cache/catalog/demo/ipod_nano_1-90x90.jpg" alt="iPod Nano" title="iPod Nano" class="img-thumbnail">
    </td>
    <td class="text-center">              
      <img src="http://demo.opencart.com/image/cache/catalog/demo/ipod_shuffle_1-90x90.jpg" alt="iPod Shuffle" title="iPod Shuffle" class="img-thumbnail">
    </td>
    <td class="text-center">              
      <img src="http://demo.opencart.com/image/cache/catalog/demo/ipod_touch_1-90x90.jpg" alt="iPod Touch" title="iPod Touch" class="img-thumbnail">
    </td>
  </tr>
  <tr>
    <td>Price
    </td>
    <td>                            $122.00                            
    </td>
    <td>                            $122.00                            
    </td>
    <td>                            $122.00                            
    </td>
    <td>                            $122.00                            
    </td>
  </tr>
  <tr>
    <td>Model
    </td>
    <td>product 20
    </td>
    <td>Product 9
    </td>
    <td>Product 7
    </td>
    <td>Product 5
    </td>
  </tr>
  <tr>
    <td>Brand
    </td>
    <td>Apple
    </td>
    <td>Apple
    </td>
    <td>Apple
    </td>
    <td>Apple
    </td>
  </tr>
  <tr>
    <td>Availability
    </td>
    <td>Out Of Stock
    </td>
    <td>2-3 Days
    </td>
    <td>2-3 Days
    </td>
    <td>2-3 Days
    </td>
  </tr>
  <tr>
    <td>Rating
    </td>
    <td class="rating">                            
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <br>
      Based on 0 reviews.
    </td>
    <td class="rating">                            
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <br>
      Based on 0 reviews.
    </td>
    <td class="rating">                            
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <br>
      Based on 0 reviews.
    </td>
    <td class="rating">                            
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <span class="fa fa-stack">
        <i class="fa fa-star-o fa-stack-2x">
        </i>
      </span>
      <br>
      Based on 0 reviews.
    </td>
  </tr>
  <tr>
    <td>Summary
    </td>
    <td class="description">
      More room to move.
      With 80GB or 160GB of storage and up to 40 hours of battery life, the new iPod classic lets you enjoy up to 40,000 songs or up to 200 hours of video or any combi..
    </td>
    <td class="description">
      Video in your pocket.
      Its the small iPod with one very big idea: video. The worlds most popular music player now lets you enjoy movies, TV shows, and more on a two-inch display thats 65% ..
    </td>
    <td class="description">
      Born to be worn.
      Clip on the worlds most wearable music player and take up to 240 songs with you anywhere. Choose from five colors including four new hues to make your musical fashion stateme..
    </td>
    <td class="description">
      Revolutionary multi-touch interface.
      iPod touch features the same multi-touch screen technology as iPhone. Pinch to zoom in on a photo. Scroll through your songs and videos with a flick. Flip thr..
    </td>
  </tr>
  <tr>
    <td>Weight
    </td>
    <td>1.00kg
    </td>
    <td>5.00kg
    </td>
    <td>5.00kg
    </td>
    <td>5.00kg
    </td>
  </tr>
  <tr>
    <td>Dimensions (L x W x H)
    </td>
    <td>0.00mm x 0.00mm x 0.00mm
    </td>
    <td>0.00mm x 0.00mm x 0.00mm
    </td>
    <td>0.00mm x 0.00mm x 0.00mm
    </td>
    <td>0.00cm x 0.00cm x 0.00cm
    </td>
  </tr>
</tbody>

产品
形象
价格
$122.00                            
$122.00                            
$122.00                            
$122.00                            
模型
产品20
产品9
产品7
产品5
烙印
苹果
苹果
苹果
苹果
可利用性
缺货
2-3天
2-3天
2-3天
评级

基于0个评论。
基于0个评论。
基于0个评论。
基于0个评论。 总结 有更多的活动空间。 新的iPod classic具有80GB或160GB的存储空间和长达40小时的电池寿命,可让您欣赏多达40000首歌曲、长达200小时的视频或任何组合。。 视频在你的口袋里。 这是一款小iPod,它有一个很大的创意:视频。世界上最流行的音乐播放器现在可以让你在65%的2英寸屏幕上欣赏电影、电视节目等。。 生来就是要穿的。 使用世界上最可穿戴的音乐播放器,随时随地播放多达240首歌曲。从五种颜色中任选一种,包括四种新色调,让您的音乐时尚更具魅力。。 革命性的多点触摸界面。 iPodtouch采用了与iPhone相同的多触摸屏技术。收缩以放大照片。使用flick滚动浏览您的歌曲和视频。翻转。。 重量 1.00公斤 5.00kg 5.00kg 5.00kg 尺寸(长x宽x高) 0.00mm x 0.00mm x 0.00mm 0.00mm x 0.00mm x 0.00mm 0.00mm x 0.00mm x 0.00mm 0.00cm x 0.00cm x 0.00cm

我想,我知道原因,但不知道如何解决…

显然你有一个比赛条件,你需要找出原因。例外情况表明

int numOfFields =   availabilityFields.size();
availabilityFields的大小已缩小

1) 这可能是循环中某些内容的副作用(尽管它看起来不会影响availabilityFields)。在这种情况下,您应该将for循环更改为

for(int i=0;i<availabilityFields.size();i++) {
for(int i=0;i)。在任何情况下:使用
iThanks,非常简单:)更改为for循环,我使用now availabilityFields.size();以及它的工作(gr8!)谢谢!:)
for(int i=0;i<availabilityFields.size();i++) {