我想在表格和后来的按钮中找到与此值相关的值。这是我的例子,我有比较产品,我想从比较产品/产品中删除"缺货"。
这是第I页工作enter link description here
这是我的代码:
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();
}
}
它有效,但根本没有,我有错误:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler.invoke(LocatingElementListHandler.java:39)
at com.sun.proxy.$Proxy6.get(Unknown Source)
at pages.HomePage.findAvailability(HomePage.java:96)
这是html:
<tbody>
<tr>
<td>Product
</td>
<td>
<a href="http://demo.opencart.com/index.php?route=product/product&product_id=48">
<strong>iPod Classic
</strong>
</a>
</td>
<td>
<a href="http://demo.opencart.com/index.php?route=product/product&product_id=36">
<strong>iPod Nano
</strong>
</a>
</td>
<td>
<a href="http://demo.opencart.com/index.php?route=product/product&product_id=34">
<strong>iPod Shuffle
</strong>
</a>
</td>
<td>
<a href="http://demo.opencart.com/index.php?route=product/product&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>
我想,我知道原因,但不知道如何解决它......
答案 0 :(得分:0)
显然你有一个竞争条件,你需要找出它的原因。
之后的例外表明int numOfFields = availabilityFields.size();
可用性的大小缩小了。
1)这可能是循环中某些东西的副作用(虽然它看起来不像会影响availabilityFields的任何东西)。在这种情况下,您应该将for循环更改为
for(int i=0;i<availabilityFields.size();i++) {
确保在每个循环开始时更新大小。
2)异常的另一个(更可能的)原因可能是另一个线程在循环时减小了数组列表的大小。在这种情况下,您可以同步块或使用java.util.Vector(已同步,请参阅此处https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html)。无论如何:使用i<availabilityFields.size()