【发布时间】: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