Java 元素不再附加到selenium webdriver中的DOM错误
首先它执行else部分,然后在打印println之后,它给出这个错误。不知道退出循环后是否出现错误。请帮忙Java 元素不再附加到selenium webdriver中的DOM错误,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,首先它执行else部分,然后在打印println之后,它给出这个错误。不知道退出循环后是否出现错误。请帮忙 Opens a page Check if the new month is available Downloads new month ex: Oct Then comes out of loop and should download Sep 但在退出循环后,它会抛出上面的错误消息 driver.get("http://www.depreportingservices.state.
Opens a page
Check if the new month is available
Downloads new month ex: Oct
Then comes out of loop and should download Sep
但在退出循环后,它会抛出上面的错误消息
driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
//maximizing the window
driver.manage().window().maximize();
//WebElement select = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));
// Select select=new Select(select);
List<WebElement> options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
//List <WebElement> Element1 = new ArrayList<WebElement>() ;
for(WebElement option : options){
if(option.getText().equals("Sep 2015 (Unconventional wells)")) {
driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
driver.wait(20000);
//options.wait(10000);
driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));
System.out.println("old month");
break;
}
else {
// if(option.getText().contains("Oct"))
//{
System.out.println("Download new month");
WebElement identifier = driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
Select select1 = new Select(identifier);
//select1.selectByVisibleText("Oct");
select1.selectByVisibleText("Oct 2015 (Unconventional wells)");
Wait(20000);
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
Wait(70000);
//Click on File save button
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
//wait time to load the options
Wait(20000);
driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
//fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
//fprofile.setPreference( "pdfjs.disabled", true );
Wait(10000);
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Oct month data downloaded in csv format");
//driver.navigate().back();
}
driver.get(“http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
//最大化窗口
driver.manage().window().maximize();
//WebElement select=driver.findElement(By.id(“ReportViewerControl\u ctl04\u ctl03\u ddValue”);
//选择=新选择(选择);
列表选项=driver.findElement(按.id(“ReportViewerControl\u ctl04\u ctl03\u ddValue”)).findElements(按.tagName(“选项”));
//List Element1=新的ArrayList();
用于(WebElement选项:选项){
如果(option.getText()等于(“2015年9月(非常规井)”){
驱动程序。获取(“http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
司机,等等(20000);
//选项。等待(10000);
driver.findElement(By.id(“ReportViewerControl_ctl04_ctl03_ddValue”);
System.out.println(“旧月”);
打破
}
否则{
//if(option.getText()包含(“Oct”))
//{
System.out.println(“下载新月份”);
WebElement标识符=driver.findElement(By.xpath(“./*[@id='ReportViewerControl\u ctl04\u ctl03\u ddValue']);
选择select1=新选择(标识符);
//选择1.选择可视文本(“Oct”);
选择1.selectByVisibleText(“2015年10月(非常规井)”);
等待(20000);
findelelement(By.xpath(“./*[@id='ReportViewerControl\u ctl04\u ctl00']”)。单击();
等待(70000);
//点击文件保存按钮
driver.findelelement(By.xpath(“./*[@id='ReportViewerControl\u ctl05\u ctl04\u ctl00\u Button']”)。单击();
//等待加载选项的时间
等待(20000);
driver.findelelement(By.xpath(“./*[@id='ReportViewerControl\u ctl05\u ctl04\u ctl00\u Menu']/div[2]/a”)。单击();
//fprofile.setPreference(“browser.download.manager.showWhenStarting”,false);
//fprofile.setPreference(“pdfjs.disabled”,true);
等待(10000);
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
System.out.println(“以csv格式下载的十月数据”);
//driver.navigate().back();
}
异常意味着您尝试使用的元素不再存在于html中
我猜您在for(WebElement option:options)
上会遇到异常,因为在else
部分中的一次单击会将您重定向到新页面或从dom中删除选项。即使返回到上一页,您仍然需要再次找到该元素
你可以试试这样的
List<WebElement> options;
int i = 0;
do
{
options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
if(options.get(i).getText().equals("Sep 2015 (Unconventional wells)"))
{
}
else()
{
}
} while (i++ < options.size())
列表选项;
int i=0;
做
{
options=driver.findElement(按.id(“ReportViewerControl_ctl04_ctl03_ddValue”)).findElements(按.tagName(“option”));
if(options.get(i).getText()等于(“2015年9月(非常规井)”)
{
}
else()
{
}
}而(i++
我可以知道如何克服这个问题吗?我想当你下载文件时,一个新的页面正在打开,它正在从dom
删除选项,那么我应该如何做到这一点呢?@user2762008将for
循环替换为do while
loop@pArAs这是可能的,但正如我所说的,我只是在猜测,这是可能的不清楚他从哪里得到异常。我使用了css,id和名称都失败了:(,但尚未更改单击此项。请帮助阅读有关页面对象设计模式的内容,测试应该像一个故事,需要非常易于阅读。阅读后,您需要投资基础设施。我很乐意帮助您感谢您的帮助,运行它等待结果这会给findElement带来错误,请放心上面的代码:stack4类型中的findElement(WebDriver,By,long,String)方法不适用于参数(WebDriver,By)
1. don't use wait
2. use css and not xpath
3 .read about page object design pattern
4 try to use this instead of the click
after you clean your code you can find the problem easily
public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds, String timeOutMessage) {
try {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(selector));
return findElement(driver, selector);
} catch (TimeoutException e) {
throw new IllegalStateException(timeOutMessage);
}
}
public static WebElement findElementSafe(WebDriver driver, By selector, long timeOutInSeconds) {
try {
return findElement(driver, selector, timeOutInSeconds);
} catch (TimeoutException e) {
return null;
}
}