0
package LoginPagePLot;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SearchRecord {
    public WebDriver driver;

    public SearchRecord(WebDriver driver1) {
        this.driver = driver1;
        WebDriverWait wait = new WebDriverWait(driver, 30);    
    }

    public void clickonuser() {
        WebElement tbody = driver.findElement(By.tagName("tbody"));
        // Now get all the TR elements from the table 
        List<WebElement> allRows = tbody.findElements(By.cssSelector("tbody>tr")); 
        System.out.println(allRows.size());

        // And iterate over them, getting the cells 
        for (WebElement row : allRows) { 
            List<WebElement> cells = row.findElements(By.tagName("td")); 

            // Print the contents of each cell
            for (WebElement cell : cells) { 
                System.out.println(cell.getText());
            }
        }
    }       
}

测试用例类

package LoginTestCasePlot;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import LoginPagePLot.AddIndividual;
import LoginPagePLot.SearchRecord;


public class VerifySearchRecord {
    public WebDriver driver;
    VerifyloginPF verifylogin=new VerifyloginPF();
    VerifyHomepage verifyhome= new VerifyHomepage();
    VerifyAddIndividual verifyadd=new VerifyAddIndividual();

    @BeforeTest
    public void verifydatainputed() {
        System.out.println("Record added");
        verifyadd.verifydashboardisplayed();
        driver=verifyadd.Adduserdata123();
        System.out.println("Record added in list");
    }
    @Test
    public void viewindividual() {
        SearchRecord srch=PageFactory.initElements(driver, SearchRecord.class);
        srch.clickonuser();
    }    
}

HTML:

<table id="myTable" class="table table-striped dataTable no-footer" role="grid" aria-describedby="myTable_info" style="width: 1061px;">
        <thead>
            <tr role="row"><th class="single-line sorting_asc" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 58px;" aria-sort="ascending" aria-label="
                    Family ID
                : activate to sort column descending">
                    Family ID
                </th><th class="single-line sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 104px;" aria-label="
                    Person Name
                : activate to sort column ascending">
                    Person Name
                </th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 94px;" aria-label="
                    CNIC
                : activate to sort column ascending">
                    CNIC
                </th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 121px;" aria-label="Registered At: activate to sort column ascending">Registered At</th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 35px;" aria-label="City: activate to sort column ascending">City</th><th class="sorting_disabled" rowspan="1" colspan="1" style="width: 57px;" aria-label="
                    Personas
                ">
                    Personas
                </th><th class="sorting_disabled" rowspan="1" colspan="1" style="width: 97px;" aria-label="Qualifications">Qualifications</th><th class="sorting" tabindex="0" aria-controls="myTable" rowspan="1" colspan="1" style="width: 40px;" aria-label="Status: activate to sort column ascending">Status</th><th class="text-center sorting_disabled" rowspan="1" colspan="1" style="width: 59px;" aria-label="Action(s)">Action(s)</th></tr>
        </thead>

000010 阿巴斯18720-9298862-8

1/22/2019 伊斯兰堡 000010 abbas18720-9198862-8

1/22/2019 伊斯兰堡 000010 abbas18721-9212843-8

1/22/2019 伊斯兰堡 000010 abbas18721-9223130-8

1/22/2019 伊斯兰堡 000010 abbas18721-9223140-8

1/22/2019 伊斯兰堡 000010 abbas18721-9298862-8

1/22/2019 伊斯兰堡 000010 abbas18721-9223110-8

1/22/2019 伊斯兰堡 000010 abbas18721-9223813-8

1/22/2019 伊斯兰堡 000010 abbas18721-9298843-8

1/22/2019 伊斯兰堡 000010 abbas18721-9223810-8

1/22/2019 伊斯兰堡

尽管表中有超过 10 行,但它始终返回 Row size=0 但我无法获得行大小,因此我无法执行测试。我已经通过 xpath、css 选择器和标记名进行了尝试,但是当我打印 row.size(); 时在行结果中没有返回任何内容;

请帮我解决这个问题。

4

1 回答 1

0

问题是因为表格是动态加载的,所以他必须等到整个表格都可用。Thread.sleep(2000) 解决了这个问题,但这不是一个好的解决方案。我建议使用 WebDriverWait 和 .until() 方法,它们将在加载元素时获取元素。

List<WebElement>trList = wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tbody, By.tagName("tr")));
于 2019-02-23T10:40:33.197 回答