【问题标题】:Unable to locate an element error is coming when i am running the below piece of code当我运行以下代码时,无法找到元素错误
【发布时间】:2017-02-23 12:30:06
【问题描述】:
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 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

【问题讨论】:

  • 任何人都可以帮助我吗?

标签: java xpath selenium-webdriver


【解决方案1】:

试试这个:

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);
   }
}

【讨论】:

    【解决方案2】:

    我试过了,但你的代码没有用,但无论如何我用谷歌搜索,我发现这个答案满足了我的需求:

    WebDriver 驱动 = 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("--------------------------------------------------");
    

    【讨论】:

      【解决方案3】:

      试试这个

      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);
      

      【讨论】:

      • 您发送的代码无法正常工作,它无限期超时,15 分钟后它给我与浏览器通信时出错
      • 跳过while循环试试这个 int x = driver.findElements(By.xpath("//*[@id='full-scorecard']/div[2]/div/table[1 ]/tbody/tr/td/a")).size() ||将返回击球手的计数
      • 嗯实际上我只需要使用 while 循环,你试过这段代码对你有用吗
      • 然后在 isDisplayed() 之前验证元素是否存在。在抛出异常时使用 try catch 块打破循环(在 catch 块中)。
      • 谁能帮帮我
      猜你喜欢
      • 2021-08-21
      • 2019-01-26
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      相关资源
      最近更新 更多