元素不再附加到DOM,Selenium WebDriver

时间:2016-03-16 08:13:47

标签: java selenium selenium-webdriver

情景:

  1. 在下拉列表中选择一个值
  2. 下载表格
  3. 走出循环
  4. 选择第二个选项
  5. 下载其他表格
  6. 但它没有在循环中选择第二个选项并给出此错误消息:

    Element is no longer attached to the DOM
    Command duration or timeout: 11 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
    Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
    System info: host: 'Treselle', ip: '192.168.0.123', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_60'
    Session ID: 8b83d2b4-acfd-4bb5-9a07-a4155e98dfc1
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=45.0}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    

1 个答案:

答案 0 :(得分:1)

如果DOM在第一次下载中发生了变化,则需要在每次迭代时重新定位下拉列表。您还应该等待报告出现,然后再次查找下拉列表

WebDriverWait wait = new WebDriverWait(driver, 20);
int size = 3;

for (int i = 2 ; i < size ; ++i) {
    WebElement dropdown = driver.findElement(...);
    Select select = new Select(dropdown);
    size = select.getOptions().size(); //change the condition to the number of options
    select.selectByIndex(i);
    driver.findElement(...).click(); //download the report
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ReportViewerControl_AsyncWait_Wait"))); //wait for the loader to appear
    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("ReportViewerControl_AsyncWait_Wait"))); //wait for the loader to disappear
}