【问题标题】:How to resolve the Exception in thread main如何解决线程main中的异常
【发布时间】:2015-09-19 23:04:44
【问题描述】:

在我下面的代码中,出现了以下错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"CarId"}
Command duration or timeout: 60.16 seconds

它适用于 For 循环的第一次迭代。对于“第二次迭代”,我得到了上述错误

我尝试使用 try catch 语句以及等待语句。

任何帮助将不胜感激。

    public class Myclass {

    static WebDriver driver = new FirefoxDriver();

    public static void main(String[] args) throws IOException, 
    JXLException,BiffException,FileNotFoundException, 
    InterruptedException, Exception {

    // driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    driver.get("url");

     //todostuff 

    FileInputStream fi = new FileInputStream("D:\\Result\\Myclass.xls");

    Workbook w = Workbook.getWorkbook(fi);

    Sheet s;

    s = w.getSheet("Sheet1");

    for(int row=1; row <=s.getRows();row++)

    {

    String ItemCode = s.getCell(0, row).getContents();

    System.out.println("Car "+CarA);

    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    driver.findElement(By.id("CarId")).clear();

     //todo stuff

    String SOH = new Myclass().getIframe("DSajdadaj");

    int y=0;

    FileOutputStream fo = new FileOutputStream("D:\\Output.xls");

    WritableWorkbook wb = Workbook.createWorkbook(fo);

    WritableSheet ws = wb.createSheet("Sheet1", 0);

    Label Sheet1 = new Label(0,y,SOH);

    ws.addCell(Sheet1);

    Thread.sleep(3000);

    System.out.println(SOH);

    Thread.sleep(1000);

    wb.write();

    wb.close();

    y++;

        }

    fi.close();

    driver.quit();

    }

    public String getIframe(String id) {

    //todo stuff

    }}

    return SOH;

    }

【问题讨论】:

  • 您的问题和代码似乎不匹配,您的代码也有问题,并且没有名为“asaas”的 id 的 findElement
  • 我现在已经更正了代码。第一次迭代进展顺利。从第二次迭代开始,for 循环代码卡住了。
  • @Madhan 另外,在将数据写入 excel 时,对于 eace 迭代,该值将被覆盖在同一个单元格中。我在 for 循环之外声明了 int y = o。我猜错误可能在“Label Sheet1 = new Label(0,y,SOH);”这一行中

标签: java selenium selenium-webdriver


【解决方案1】:

从您的代码看来,您正在切换到getIframe 中的一个框架。但是操作完成后忘记切换到默认值[driver.switchTo().defaultContent();]

  public String getIframe(String id) {
    String Value = "";
    final List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
    for (WebElement iframe : iframes) {
         if (iframe.getAttribute("id").equals(id)) {
            driver.switchTo().frame(id);//switch happens
            Value = driver.findElement(By.xpathdfdgdg")).getText();
            System.out.println("erer" + Value);
        }
    }
    return Value;
}


   for (int row = 1; row <= s.getRows(); row++) {

        String ItemCode = s.getCell(0, row).getContents();

        System.out.println("Car " + CarA);

       // driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  No need to put implicit wait in for loop.It can be outside as well
        driver.switchTo().defaultContent();//Have to switch to the default because the element is in the top window not inside frame
        driver.findElement(By.id("CarId")).clear();

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2015-06-13
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多