gpt4 book ai didi

java - 网络驱动程序java。大型网络表,如何快速找到特定值

转载 作者:行者123 更新时间:2023-12-01 13:34:24 24 4
gpt4 key购买 nike

在一个非常大的网络表上,通常有数百行和许多列,我们如何才能快速找到特定值?理想情况下,我想指定一个列名称,仅查找该列的单元格数据并检索指定的单元格值。

我的代码很慢,它从表中的所有单元格中获取所有数据,然后验证每个单元格以找到指定的值。这会消耗大量时间。

public static void selectColumnValue(String tableXpath, String columnValue){ 
String str, found = "false";
// String colValue = null;
WebElement table = driver.findElement(By.xpath(tableXpath));
List<WebElement> rows=table.findElements(By.xpath(tableXpath+"/tbody/tr"));
for(WebElement row:rows)
{
List<WebElement> cols =row.findElements(By.xpath("td"));
for(WebElement col:cols)
{
str=col.getText().trim();
//System.out.println(str);
if(str.matches(columnValue))
{
//List<WebElement> vals=row.findElements(By.xpath("td"));
col.click();
found="true";

}
}
if(found.matches("true")){
break;
} } }

几行的 HTML 如下:

    <table id="T536870949" class="BaseTable" title="" style="top: 16px;">
<colgroup cols="5">
<col style="width: 139px;"/>
<col style="width: 140px;"/>
<col style="width: 139px;"/>
<col style="width: 140px;"/>
<col style="width: 119px;"/>
</colgroup>
<tbody>
<tr class="hiddentablehdr">
<th scope="col">Device Name</th>
<th scope="col">Site ID</th>
<th scope="col">Monitoring SSA</th>
<th scope="col">Manufacturer</th>
<th scope="col">Model</th>
</tr>
<tr class="" arrow="0" tabindex="0">
<td class="BaseTableCellOdd BaseTableCellOddColor BaseTableStaticText" scope="row" style="width: 139px;" title="001000">
<nobr class="dp " style="text-align: right; width: 139px;">
</td>
<td class="BaseTableCellOdd BaseTableCellOddColor BaseTableStaticText" style="width: 140px;">
<nobr class="dp " style="text-align: right; width: 140px;">
<span style="float:left;">NTA001</span>
</nobr>
</td>
<td class="BaseTableCellOdd BaseTableCellOddColor BaseTableStaticText" style="width: 139px;" title="0019839">
<nobr class="dp " style="text-align: right; width: 139px;">
<span style="float:left;">0019839</span>
</nobr>
</td>
<td class="BaseTableCellOdd BaseTableCellOddColor BaseTableStaticText" style="width: 140px;">
<nobr class="dp " style="text-align: right; width: 140px;">
<span style="float:left;">unknown</span>
</nobr>
</td>
<td class="BaseTableCellOdd BaseTableCellOddColor BaseTableStaticText" style="width: 119px;">
</tr>
<tr class="" arrow="1" tabindex="0">
<td class="BaseTableCell BaseTableCellColor BaseTableStaticText" scope="row" style="width: 139px;" title="001001">
<nobr class="dp " style="text-align: right; width: 139px;">
<span style="float:left;">001001</span>
</nobr>
</td>
<td class="BaseTableCell BaseTableCellColor BaseTableStaticText" style="width: 140px;" title="001">
<nobr class="dp " style="text-align: right; width: 140px;">
<span style="float:left;">001</span>
</nobr>
</td>
<td class="BaseTableCell BaseTableCellColor BaseTableStaticText" style="width: 139px;" title="0019839">
<nobr class="dp " style="text-align: right; width: 139px;">
<span style="float:left;">0019839</span>
</nobr>
</td>
<td class="BaseTableCell BaseTableCellColor BaseTableStaticText" style="width: 140px;">
<nobr class="dp " style="text-align: right; width: 140px;">
<span style="float:left;">DMI</span>
</nobr>
</td>
<td class="BaseTableCell BaseTableCellColor BaseTableStaticText" style="width: 119px;">
<nobr class="dp " style="text-align: right; width: 119px;">
<span style="float:left;">Manufacturer=xyz</span>
</nobr>
</td>
</tr>

........

最佳答案

您可以搜索您感兴趣的元素的完整 xpath,而不是循环所有元素,例如:

 String searchedXpath = tableXpath + "/tbody/tr/td[span='" + columnValue + "']/span";
try {
WebElement col = driver.findElement(By.xpath(searchedXpath ));
System.out.println("Value Found: " + col.getText());
}
catch (NoSuchElementException e) {
System.out.println("No such value in the table");
}

当然,条件 [span='"+ columnValue + "'] 可以调整为您认为正确的任何内容(例如可以是 [contains(span,columnValue)] )

关于java - 网络驱动程序java。大型网络表,如何快速找到特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21385223/

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