本文介绍了如何解决线程主异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my below code, the following error arised:

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

它对于For循环的第一次迭代效果很好.对于第二次迭代",我遇到了以上错误

It works well for the first iteration of For loop. For the "second iteration" I got the above error

我尝试使用try catch语句以及wait语句.

I tried using try catch statement and also the wait statements.

任何帮助将不胜感激.

    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;

    }

推荐答案

从您的代码看来,您正在切换到getIframe中的帧.但是一旦操作完成,就忘了切换到默认值[driver.switchTo().defaultContent();]

From your code it seems you are switching to a frame in getIframe. But forgot to switch to default[ driver.switchTo().defaultContent();] once your opertation is done

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

这篇关于如何解决线程主异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:54