当我尝试通过 selenium 中的 WebTable 时,总是给我 0 的行数
Always gives me row count of 0 when I try to get through WebTable in selenium
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());
}
}
}
}
测试用例class
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/2019Islambad 000010 abbas18720-9198862-81/22/2019Islambad 000010 abbas18721-9212843-81/22/2019abb10islambad10 9223130-81/22/2019伊斯兰巴德 000010 abbas18721-9223140-81/22/2019伊斯兰巴德 000010 abbas18721-9298862-81/22/2019伊斯兰巴德 11902011 01902019 81/22/2019Islambad 000010 abbas18721-9223813-81/22/2019Islambad 000010 abbas18721-9298843-81/22/2019Islambad 000010 abbas1-92818[3872 =13=]1/22/2019伊斯兰堡
它总是returns行大小=0,虽然有超过10行
table 但是我无法获得行大小,因此我无法
执行我的测试。我已经通过 xpath、css 选择器和
tagname 但 returns 当我打印 row.size();
时,行中没有任何结果
请帮我解决这个问题。
问题是因为 table 是动态加载的,所以他不得不等到整个 table 可用。 Thread.sleep(2000) 解决了这个问题,但不是很好的解决方案。
我建议使用 WebDriverWait 和 .until() 方法,它们将在加载元素时获取元素。
List<WebElement>trList = wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tbody, By.tagName("tr")));
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());
}
}
}
}
测试用例class
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/2019Islambad 000010 abbas18720-9198862-81/22/2019Islambad 000010 abbas18721-9212843-81/22/2019abb10islambad10 9223130-81/22/2019伊斯兰巴德 000010 abbas18721-9223140-81/22/2019伊斯兰巴德 000010 abbas18721-9298862-81/22/2019伊斯兰巴德 11902011 01902019 81/22/2019Islambad 000010 abbas18721-9223813-81/22/2019Islambad 000010 abbas18721-9298843-81/22/2019Islambad 000010 abbas1-92818[3872 =13=]1/22/2019伊斯兰堡它总是returns行大小=0,虽然有超过10行 table 但是我无法获得行大小,因此我无法 执行我的测试。我已经通过 xpath、css 选择器和 tagname 但 returns 当我打印 row.size();
时,行中没有任何结果请帮我解决这个问题。
问题是因为 table 是动态加载的,所以他不得不等到整个 table 可用。 Thread.sleep(2000) 解决了这个问题,但不是很好的解决方案。 我建议使用 WebDriverWait 和 .until() 方法,它们将在加载元素时获取元素。
List<WebElement>trList = wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tbody, By.tagName("tr")));