如何处理Selenium的TimeoutException?pageLoadTimeout未生效问题
问题:Selenium点击下载按钮后未按指定pageLoadTimeout等待即返回成功
场景
需从网站下载文件
当前情况
- 点击下载按钮后,页面加载时长超6-7分钟(为应用自身问题),之后才开始下载。
- Selenium默认
pageLoadTimeout为300秒,5分钟后抛出如下错误:
org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 300.000
需求
因是应用问题,不想等待默认超时完成,需在指定时间内页面未加载则抛出错误。
已实现代码
编写了clickElement方法,通过修改pageLoadTimeout设置超时时间,操作完成后恢复为默认300秒,代码如下:
public boolean clickElement(WebElement element, int timeOut, String message) { try { wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.elementToBeClickable(element)); driver.manage().timeouts().pageLoadTimeout(timeOut, TimeUnit.SECONDS); element.click(); return true; } catch (org.openqa.selenium.TimeoutException e1) { Reports.logFail(message); } finally { driver.manage().timeouts().pageLoadTimeout(300, TimeUnit.SECONDS); } return false; }
遇到的问题
执行element.click();后,WebDriver未等待指定时长,即便页面未加载完成也立即返回true。
内容的提问来源于stack exchange,提问作者Nandan A
相关产品推荐
相关产品推荐

