当前位置: 首页 > 知识库问答 >
问题:

当我运行下面的代码时,无法找到一个元素错误

单于楚
2023-03-14
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/table[1]/tbody/tr[20]/td[2]/a”}命令持续时间或超时:40毫秒有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html构建信息:版本:'2.53.1',修订:'A36B8B1',时间:'2016-06-30'192.168.0.14',os.name:'Windows7',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0_101'驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver

共有1个答案

公孙国兴
2023-03-14

试试这个

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);
 类似资料: