睡眠中断异常:java.lang.InterruptedException:睡眠中断 - 如何为浏览器提供 40 分钟超时

Sleep Interupt Exception: java.lang.InterruptedException: sleep interrupted - How to give timeout for browser for 40mins

我正在尝试从基于 Web 的应用程序下载 excel 报告,在我单击 'download' 按钮后需要 30 分钟才能开始下载(需要 30 分钟才能生成并开始下载)。该文件的大小约为 54 MB。下面是我的代码:

driver().findElement(By.xpath(locator_for_download)).click(); //to click on download button
TimeUnit.SECONDS.sleep(2400); //making it to sleep for 40 mins so that my report gets downloaded.

但是,一旦 'download' 按钮被点击,5 分钟后我得到以下错误并且浏览器关闭:

Exception: java.lang.InterruptedException: sleep interrupted

我试过隐式等待 2400 秒:

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS); 

还有页面加载超时 none 有效。

P.S - 对于我们对其执行的任何操作,URL 都没有变化。 Web 应用程序有 iframe,我在 Google chrome 浏览器中进行自动化。

任何人都可以帮助我如何下载我的报告,因为睡眠对我不起作用?是不是因为浏览器超时时间少了被强行关闭了?非常感谢任何帮助。

我认为你是运行节点上的脚本(grid/node模型),如果答案是肯定的。然后它解释了为什么您会收到此异常。

在您的情况下,睡眠时间 > 节点超时,这是导致异常的原因。节点的默认超时为 300 秒(5 分钟),这意味着如果节点在 5 分钟内未收到命令,则集线器将自动释放该节点。如果您想等待超过 5 分钟,则必须更改 set -timeout = 0(以完全删除超时)或根据您的情况增加它。

有关详细信息,请参阅 SeleniumHQ Grid documentation here

Timeout 的解决方法是在等待时定期在页面上执行一些操作。
例如,下面的代码每 2 分钟在 HTML 标记(或任何其他元素)上按 Tab 按钮 20 分钟以防止任何超时。

driver.findElement(By.xpath(locator_for_download)).click();
for (int i = 0; i < 20; i++) {
    // Break if file downloaded before a timeout, fileDownloaded(myFileName) is example method name to check if file is downloaded.
    if (fileDownloaded(myFileName))
        break;
    
    TimeUnit.MINUTES.sleep(2);
    driver.findElement(By.tagName("html")).sendKeys(Keys.TAB);
}

if (fileDownloaded(myFileName))
    System.out.println("File downloaded successfully");
else
    System.out.println("File download failed");