从每个 table 行中获取值

Get values from each table rows

我使用此代码验证 table 单行:

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']/div/div"));
        for (WebElement e : tableRows) {
            if (e.getAttribute("col-id").equalsIgnoreCase("projectLookupCodes")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("loadContainerSequence")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("67", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("packagedItemCount")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("23", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("locationName")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("Location", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("licensePlateLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("LP-12160", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("shipmentLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("520031", e.getText());
            }
        }

HTML代码:

https://pastebin.com/TCRVLvpf

如何验证每个 table 行?目前它正在搜索第一行等等。如何扩展此代码以搜索多个 table 行值。

看看这是否有效:-

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']/div"));
        String projectLookupCodes = ".//div[@col-id='projectLookupCodes']";
        String loadContainerSequence = ".//div[@col-id='loadContainerSequence']";
        String packagedItemCount = ".//div[@col-id='packagedItemCount']";
        String locationName = ".//div[@col-id='locationName']";
        String licensePlateLookupCode = ".//div[@col-id='licensePlateLookupCode']";
        String shipmentLookupCode = ".//div[@col-id='shipmentLookupCode']";
        
        for(WebElement e: tableRows) {
            if(e.findElement(By.xpath(projectLookupCodes)).isDisplayed()) {
                System.out.println("ProjectLookupCodes: "+e.findElement(By.xpath(projectLookupCodes)).getText());
            }
            if(e.findElement(By.xpath(loadContainerSequence)).isDisplayed()) {
                System.out.println("loadContainerSequence: "+e.findElement(By.xpath(loadContainerSequence)).getText());
            }
            if(e.findElement(By.xpath(packagedItemCount)).isDisplayed()) {
                System.out.println("packagedItemCount: "+e.findElement(By.xpath(packagedItemCount)).getText());
            }
            if(e.findElement(By.xpath(locationName)).isDisplayed()) {
                System.out.println("locationName: "+e.findElement(By.xpath(locationName)).getText());
            }
            if(e.findElement(By.xpath(licensePlateLookupCode)).isDisplayed()) {
                System.out.println("licensePlateLookupCode: "+e.findElement(By.xpath(licensePlateLookupCode)).getText());
            }
            if(e.findElement(By.xpath(shipmentLookupCode)).isDisplayed()) {
                System.out.println("shipmentLookupCode: "+e.findElement(By.xpath(shipmentLookupCode)).getText());
            }
            System.out.println("=============================");
        }

据我所知,您已经非常接近解决方案了。
您所需要做的就是从 table 中检索所有行,并为每一行找到它的所有单元格。
现在您可以迭代内部循环中的每个单元格并验证其值。

List<WebElement> tableRows = driver.findElements(By.xpath(".//div[@class='ag-center-cols-container']//div[@role='row']"));
    for(WebElement row :tableRows){
        List<WebElement> rowCells = row.findElements(By.xpath(".//div"));
        for (WebElement e : rowCells) {
            if (e.getAttribute("col-id").equalsIgnoreCase("projectLookupCodes")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("loadContainerSequence")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("67", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("packagedItemCount")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("23", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("locationName")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("Location", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("licensePlateLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("LP-12160", e.getText());
            }
            if (e.getAttribute("col-id").equalsIgnoreCase("shipmentLookupCode")){
                System.out.println("AssertEquals value " + e.getText());
                assertEquals("520031", e.getText());
            }
        
    }