硒动态增长 table
selenium dynamic growing table
我在使用 selenium 处理动态增长 table 时遇到了一些麻烦。
快速总结一下,在我的网络应用程序中,我有一个包含 30 个项目的 table,但它只显示前二十个项目,我们必须向下滚动才能显示其余项目。
而且我不知道如何在不向下滚动的情况下获得第 26 个(例如)项目。
我的HTML:
<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
<thead>
<tbody id="tableID-tblBody">
<tr id="item1" role="row" tabindex="-1">
<tr id="item2" role="row" tabindex="-1">
[... 17 more]
<tr id="item20" role="row" tabindex="-1">
</tbody>
</table>
滚动后:
<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
<thead>
<tbody id="tableID-tblBody">
<tr id="item1" role="row" tabindex="-1">
<tr id="item2" role="row" tabindex="-1">
[... 27 more]
<tr id="item30" role="row" tabindex="-1">
</tbody>
</table>
如果有人能帮助我就太好了^^
谢谢!
我会 scroll into view table 中的最后一行(如果需要多次,直到获得所需的行数)。为此,我将通过 executeScript()
:
使用 scrollIntoView()
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView();", element);
其中 element
:
WebElement element = driver.findElements(By.xpath("//table[@id = 'tableID']//tr[last()]")).
如果不向下滚动(要向下滚动使用@alecxe 解决方案),您将无法获得该项目,因为您要选择的元素在您搜索时甚至不在 html 中。
滚动后你应该可以很容易地通过id找到它。
我认为您正在尝试比较值
Expected Value V/s Actual Value.
要做到这一点,我会要求您获取此 table 下的所有 elements/object:tableID-tblBody 并搜索 ID 26 并取值。
这是我用来执行类似任务的代码。
WebDriverWait wait = new WebDriverWait(driver, timeout);
List<WebElement> totalrow = wait.until(ExpectedConditions
.presenceOfAllElementsLocatedBy(By.Id("tableID-tblBody"));
for (int i = 0; i <= totalrow.size(); i++) {
String xPath ="" //"XPATH OF 26 ITEM";
try {
WebElement element = ExpectedBehaviors
.expectPresenceofElementBy(driver, By.xpath(xPath),
getAlertTimeout());
if (element.getText().trim().equals("EXPECTED VALUE")) {
//"PERFORM YOUR ACTION"
break;
}
} catch (Exception e) {
//"You can add your exception and error message
}
}
我在使用 selenium 处理动态增长 table 时遇到了一些麻烦。 快速总结一下,在我的网络应用程序中,我有一个包含 30 个项目的 table,但它只显示前二十个项目,我们必须向下滚动才能显示其余项目。 而且我不知道如何在不向下滚动的情况下获得第 26 个(例如)项目。
我的HTML:
<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
<thead>
<tbody id="tableID-tblBody">
<tr id="item1" role="row" tabindex="-1">
<tr id="item2" role="row" tabindex="-1">
[... 17 more]
<tr id="item20" role="row" tabindex="-1">
</tbody>
</table>
滚动后:
<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
<thead>
<tbody id="tableID-tblBody">
<tr id="item1" role="row" tabindex="-1">
<tr id="item2" role="row" tabindex="-1">
[... 27 more]
<tr id="item30" role="row" tabindex="-1">
</tbody>
</table>
如果有人能帮助我就太好了^^
谢谢!
我会 scroll into view table 中的最后一行(如果需要多次,直到获得所需的行数)。为此,我将通过 executeScript()
:
scrollIntoView()
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView();", element);
其中 element
:
WebElement element = driver.findElements(By.xpath("//table[@id = 'tableID']//tr[last()]")).
如果不向下滚动(要向下滚动使用@alecxe 解决方案),您将无法获得该项目,因为您要选择的元素在您搜索时甚至不在 html 中。 滚动后你应该可以很容易地通过id找到它。
我认为您正在尝试比较值
Expected Value V/s Actual Value.
要做到这一点,我会要求您获取此 table 下的所有 elements/object:tableID-tblBody 并搜索 ID 26 并取值。
这是我用来执行类似任务的代码。
WebDriverWait wait = new WebDriverWait(driver, timeout);
List<WebElement> totalrow = wait.until(ExpectedConditions
.presenceOfAllElementsLocatedBy(By.Id("tableID-tblBody"));
for (int i = 0; i <= totalrow.size(); i++) {
String xPath ="" //"XPATH OF 26 ITEM";
try {
WebElement element = ExpectedBehaviors
.expectPresenceofElementBy(driver, By.xpath(xPath),
getAlertTimeout());
if (element.getText().trim().equals("EXPECTED VALUE")) {
//"PERFORM YOUR ACTION"
break;
}
} catch (Exception e) {
//"You can add your exception and error message
}
}