gpt4 book ai didi

java - 如何跳过表格行

转载 作者:行者123 更新时间:2023-12-01 11:56:54 26 4
gpt4 key购买 nike

我创建了一个小脚本,它在其中循环并删除表中不需要的行,但表中有一行无法删除。如何跳过该行并转到下一行?

这是我的脚本:

for(int i=0; i<25; i++){ 

if(driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).getText().contains("Skip Me")){
//what to add here to skip the "Skip Me" text????
}

//select the item in the table
driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).click();

//click the delete button
driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click();

这就是该列的样子。我想跳过 RealMedia,然后删除前后的所有项目。

enter image description here

HTML:

<table class="table smallInput dataTable" id="dataTableAdvertisers" ">
<thead>
<tr role="row" style="height: 0px;">
<th class="sorting_asc" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
</tr>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<td class="">
<a href="getadvertiserdetailsNew.do?advertiserKey=198909">RealMedia</a></td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
</tr><tr class="even">
<td class="">
<a href="getadvertiserdetailsNew.do?advertiserKey=198910">teset2</a></td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td><td class="">---</td>
</tr><tr class="odd">

</tbody>
</table>

最佳答案

尝试以下操作:确保在获取列表之前有一些等待(如果需要)。此元素列表将找到该表下的所有 a 标记,并且 for 循环遍历集合并删除集合中不具有文本匹配 的任何成员>RealMedia。您不应该盲目地设置迭代器的上限。这将使程序不必要地循环,这是一个不好的做法。

List<WebElement> elements = driver.findElements(By.cssSelector("#dataTableAdvertisers a"));

for (WebElement element: elements){
if (!element.getText().contains("RealMedia")){
//select the item in the table
driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).click();

//click the delete button
driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click();
}
}

编辑:

By selector  = By.cssSelector("#dataTableAdvertisers a");
List<WebElement> elements = driver.findElements(selector);

//This just controls the loop. Iterating through the collection will return StaleElement ref exception
for (int i = 0; i<elements.size(); i++){

//Just want to delete the first item on the list
By xpath = By.xpath("//table[@id='dataTableAdvertisers']//a[not(.='RealMedia')]");

if (driver.findElements(xpath).size()>0){
WebElement element = driver.findElements(xpath).get(0);

element.click();

//click the delete button
driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click();
}
}

关于java - 如何跳过表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com