在主窗口上,我试图点击一个打开弹出窗口的链接。我从该窗口获取文本然后关闭它。 我再次在主窗口上重复大约40个链接。
但是,经过3-4次循环后,失败了。不知道为什么会这样。大多数情况下,它发生在'if'条件不符合的情况下,但也可能发生。
我收到以下错误:
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);
}
答案 0 :(得分:0)
正如@aimbire在评论中建议的那样,您应该尽可能避免使用Thread.Sleep
。相反,我建议使用WebDriverWait class。使用此功能,您可以设置最大超时和条件(例如,页面是否包含您的文本)。使用睡眠意味着如果服务器在每次迭代测试时都没有在相同的时间内完成操作,那么就会出现诸如您所面临的不一致之处。
答案 1 :(得分:0)
如果使用正常的硒等问题仍然存在,请尝试使用流利的等待。