gpt4 book ai didi

java - 如何在分页式Web表格中找到所需的值?

转载 作者:行者123 更新时间:2023-12-01 17:16:05 25 4
gpt4 key购买 nike

该页面具有分页的Web表格。我想查看表格并检查是否没有包含来自param的文本的单元格-单击下一步按钮,然后再次检查

我试图写类似的东西。我写了遍历行列表并从每行中指定列下的单元格获取文本的方法

protected GroupTableContainerComponent getContainerComponentByName(String groupCode) {
GroupTableContainerComponent result = null;
List<GroupTableContainerComponent> rows = getContainerComponents();
Iterator<GroupTableContainerComponent> i = rows.iterator();
while (i.hasNext()) {
GroupTableContainerComponent current = i.next();

if (!current.getGroupCodeText().toLowerCase().equals(groupCode.toLowerCase())) {
clickNextButton();

List<GroupTableContainerComponent> rows2 = getContainerComponents();
Iterator<GroupTableContainerComponent> j = rows2.iterator();
while (j.hasNext()){
GroupTableContainerComponent current2 = j.next();
if (current2.getGroupCodeText().toLowerCase().equals(groupCode.toLowerCase())){

}
}
}
result = current;
break;
}
if (result == null) {
throw new RuntimeException(String.format("Group with groupCode: %s not found", groupCode));
}
return result;
}


但是单击下一步按钮后,我得到错误StaleElementReferenceException

我认为这是因为我的行列表包含表上一页的旧行

这是我的getContainerComponent()

public List<GroupTableContainerComponent> getContainerComponents() {
containerComponents = new ArrayList<>();

for (WebElement current : driver.findElements(By.xpath(groupTableContainerXpath))) {
containerComponents.add(new GroupTableContainerComponent(current));
}
return containerComponents;
}


和Xpath

private final String groupTableContainerXpath = "//div[@class='table-container']/table/tbody/tr";


还有我的getGroupCodeText()

public WebElement getGroupCode() {
WebElement groupsCode = groupTableComponent.findElement(groupCode);
return groupsCode;
}

public String getGroupCodeText() {
return getGroupCode().getText();
}


和Xpath

private  By groupCode = By.xpath(".//td[contains(@class,'cdk-column-group_name')]");

最佳答案

我做到了,我决定在我的方法中创建列表。首先,我检查表格第一页中是否存在带参数的文本的单元格,如果该单元格不存在,我检查分页下一个按钮的状态,并在启用该功能时单击它,并等待第一个列表不可见,然后创建新列表并分配新值

protected GroupTableContainerComponent getContainerComponentByName(String groupCode) {
GroupTableContainerComponent result = null;
WebDriverWait waits = new WebDriverWait(driver, 10);
List<WebElement> rows = driver.findElements(By.xpath(groupTableContainerXpath));
List<GroupTableContainerComponent> containerComponents = new ArrayList<>();
for (WebElement current : rows) {
containerComponents.add(new GroupTableContainerComponent(current));
}
for (GroupTableContainerComponent current : containerComponents) {
System.out.println(current.getGroupCodeText());
if (current.getGroupCodeText().toLowerCase().equals(groupCode.toLowerCase())) {
result = current;
break;
}

}
if (result == null){

boolean isEnabled = isNextArrowEnabled();
while (isEnabled) {
clickNextButton();
waits.until(ExpectedConditions.invisibilityOfAllElements(rows));
List<WebElement> rows2 = waits.until(ExpectedConditions.visibilityOfAllElements(driver.findElements(By.xpath(groupTableContainerXpath))));
List<GroupTableContainerComponent> containerComponents2 = new ArrayList<>();
for (WebElement current2 : rows2) {
containerComponents2.add(new GroupTableContainerComponent(current2));

}
for (GroupTableContainerComponent current3 : containerComponents2) {
System.out.println(current3.getGroupCodeText());
if (current3.getGroupCodeText().toLowerCase().equals(groupCode.toLowerCase())) {
result = current3;
break;
}
}

if (result == null) {
if (isNextArrowEnabled()) isEnabled = true;
else isEnabled = false;
} else return result;
}
}

if (result == null) {
throw new RuntimeException(String.format("Group with groupCode: %s not found", groupCode));
}

return result;

关于java - 如何在分页式Web表格中找到所需的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61384779/

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