在主窗口上,我试图单击打开弹出窗口的链接。我从该窗口获取文本,然后将其关闭。我再次对主窗口上的大约 40 个链接重复此操作。
但是,经过 3-4 次循环后,它失败了。不知道为什么会这样。大多数情况下,它发生在“如果”条件不满足时,但也可能在其他情况下发生。
我收到以下错误:
java.util.NoSuchElementException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
这是我的代码片段:
for(int x=1;x<119;x+=3)
{
//waitforelement.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='resultsTable']/tbody/tr["+x+"]/td[2]/a")));
WebElement element = driver.findElement(By.xpath("//*[@id='resultsTable']/tbody/tr["+x+"]/td[2]/a"));
Actions builder = new Actions(driver);
builder.moveToElement(element).click().build().perform();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Set <String> windowids = driver.getWindowHandles();
Iterator <String> iterat = windowids.iterator();
String mainwindowid = iterat.next();
String next1stwindowid = iterat.next();
driver.switchTo().window(next1stwindowid);
String pageSource = driver.getPageSource();
Thread.sleep(750);
if(!pageSource.contains("Resume is no longer available."))
{
String Name = driver.findElement(By.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[1]/td[1]/div[2]/span[1]")).getText();
String Phone = driver.findElement(By.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[2]/td")).getText();
String Email = driver.findElement(By.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[3]/td")).getText();
System.out.println("On page "+i+", the detail for list number "+x + "\t candidate is: " +Name +"\t"+ Phone+"\t"+Email);
}
Thread.sleep(750);
driver.close();
Thread.sleep(1000);
driver.switchTo().window(mainwindowid);
}