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 abbas18720-9298862-8
1/22/2019伊斯拉巴德000010 abbas18720-9198862-81/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 / 2019Islambad 000010 abbas18721-9223110-8
1/22 / 2019Islambad 000010 abbas18721-9223813-8
1/22 / 2019Islambad 000010 abbas18721-9298843-8
< / p> 2019年1月22日伊斯兰堡000010 abbas18721-9223810-8
2019年2月2日伊斯兰堡
它总是返回行大小= 0,尽管其中有10行以上 表,但是由于行不通,我无法获取行大小 执行我的测试。我已经尝试通过xpath,css选择器和 标记名,但是当我打印row.size();
时,在行结果中什么也不返回请帮助我解决此问题。
答案 0 :(得分:0)
问题是因为表是动态加载的,所以他不得不等到整个表可用为止。 Thread.sleep(2000)解决了此问题,但这不是一个好的解决方案。 我建议使用WebDriverWait和.until()方法,这些方法将在加载元素时获取元素。
List<WebElement>trList = wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tbody, By.tagName("tr")));