public class TestWebtable {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.espncricinfo.com/indian-premier-league-2016/engine/match/981019.html");
int i = 2;
int rowNum = 0;
while (driver
.findElement(
By.xpath(".//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr["
+ i + "]/td[2]/a")).isDisplayed()) {
i = i + 2;
rowNum++;
}
System.out.println("Total rows are : " + rowNum);
}}
线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法找到元素:{“method”:“xpath”,“selector”:“.//* [@ id ='full-scorecard'] / DIV [2] / DIV /表[1] / tbody的/ TR [20] / TD [2] / A“} 命令持续时间或超时:40毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'2.53.1',修订版:'a36b8b1',时间:'2016-06-30 17:32:46' 系统信息:主机:'pc-PC',ip:'192.168.0.14',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8 .0_101' 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
答案 0 :(得分:0)
试试这个
driver.get("http://www.espncricinfo.com/indian-premier-league-2016/engine/match/981019.html");
int i = 2;
int rowNum = 0;
while (true) {
try {
driver.findElement(
By.xpath(".//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr[" + i + "]/td[2]/a"));
i = i + 2;
rowNum++;
} catch (Exception e) {
break;
}
}
System.out.println("Total rows are : " + rowNum);
答案 1 :(得分:0)
我试过但是你的代码没有用,但无论如何我用Google搜索,我发现这个答案满足了我的需求:
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.espncricinfo.com/indian-premier-league-2016/engine/match/981019.html");
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void Handle_Dynamic_Webtable() {
//To locate table.
WebElement mytable = driver.findElement(By.xpath(".//*[@id='full-scorecard']/div[2]/div/table[1][@class='batting-table innings']"));
//To locate rows of table.
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
//To calculate no of rows In table.
int rows_count = rows_table.size();
//Loop will execute till the last row of table.
for (int row=0; row<rows_count; row++){
//To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
//Loop will execute till the last cell of that specific row.
for (int column=0; column<columns_count; column++){
//To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
}
System.out.println("--------------------------------------------------");
答案 2 :(得分:0)
试试这个:
public class TestWebtable {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.espncricinfo.com/indian-premier-league-2016/engine/match/981019.html");
int rowNum = 0;
List<WebElement> lstElements = driver.findElements(By.XPath(".//*[@id='full-scorecard']/div[2]/div/table[1]/tbody/tr/td[contains(@class,'batsman-name')]/a"));
rowNum = lstElements.Count;
System.out.println("Total rows are : " + rowNum);
}
}