Java 如何解决线程main中的异常
在我下面的代码中,出现了以下错误:Java 如何解决线程main中的异常,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,在我下面的代码中,出现了以下错误: 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语句和wa
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语句和wait语句
任何帮助都将不胜感激
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;]
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();
您的问题和代码似乎不匹配,您的代码也有缺陷,并且没有名为'asaas'的id的findElement,我现在已经更正了代码。第一次迭代进行得很顺利。从第二次迭代开始,for循环代码就一直处于关闭状态。@Madhan在将数据写入excel时,对于每次迭代,值在同一单元格中被覆盖。我在for循环外声明了int y=o。我猜错误可能在线标签Sheet1=新标签0,y,SOH;