selenium - 等到元素可点击未正确超时
问题描述
我在处理超时时遇到了一些问题,因为它似乎并非在所有情况下都有效。我已将超时定义如下:
wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(60))
.pollingEvery(Duration.ofSeconds(1))
.ignoring(NoSuchElementException.class);
现在,当我想等到页面上出现一个元素时,我使用这段代码:
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
它适用于大多数情况(等待元素并在 60 秒后超时),但最近我们遇到了一些页面卡住加载的问题(在左下角有一条消息等待 ...页)。发生这种情况时,我意识到这段代码不能正常工作。它不会在 60 秒后超时,而是在 10 分钟后超时。
编辑:实际上,试图进一步调查我的问题,我意识到它确实来自另一行也包含等待的代码:
wait.until(ExpectedConditions.elementToBeClickable(locator));
基本上,我单击重定向到另一个页面的链接,等待按钮出现,等待按钮可点击,然后单击按钮。所以它检测到按钮存在,但它会等待它是可点击的,并且在 60 秒后不会超时。
所以当我定义我的驱动程序时,我添加了以下行:
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
在控制台中,我看到以下行: Timed out received message from renderer: 60.000
但是我如何捕捉到这个异常呢?我试图用 try/catch 来包围我的等待,但它不起作用。
try {
wait.until(ExpectedConditions.elementToBeClickable(locator));
} catch (Exception e) {
logger.info("TEST");
throw new TimeoutException("element " + locator.toString() + " not found on the page");
}
我该怎么做?谢谢。
解决方案
您需要注意以下几点:
- 如果您的用例是获取元素的任何属性(例如classname、innerHTML等)或在元素上调用,那么等到元素出现在页面上理想情况下不会有太大帮助
click()
。 - 相反,根据您的用例,您需要使用visibilityOfElementLocated()或elementToBeClickable()。
- 您可以在以下位置找到一些详细的讨论:
- 代码行在60 秒
wait.until(ExpectedConditions.presenceOfElementLocated(locator))
后没有超时但在10 分钟后超时的原因可能是由于WebDriverWait您还配置了隐式等待,并且根据文档混淆隐式等待和显式等待可能导致不可预知的等待时间 要配置pageLoadTimeout,您可以使用以下代码块:
代码块:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS); try{ driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl"); }catch(WebDriverException e){ System.out.println("WebDriverException occured"); } driver.quit();
控制台输出:
INFO: Detected dialect: W3C [1563377008.449][SEVERE]: Timed out receiving message from renderer: 1.999 [1563377008.450][SEVERE]: Timed out receiving message from renderer: -0.001 [1563377008.461][SEVERE]: Timed out receiving message from renderer: -0.012 [1563377010.466][SEVERE]: Timed out receiving message from renderer: 1.998 [1563377010.467][SEVERE]: Timed out receiving message from renderer: -0.001 [1563377010.476][SEVERE]: Timed out receiving message from renderer: -0.010 WebDriverException occured
您可以在 Selenium not working的 pageLoadTimeout 中找到相关的详细讨论
推荐阅读
- javascript - 如何使用输入范围更改下方画布上笔的粗细?
- vba - 如何使公式适用于除 Excel 中的第一个工作表之外的所有工作表
- matlab - 一类学习使用 MATLAB 进行预测
- django - Django:使用Javascript确认删除视图而不重定向到模板
- python - PyMySQL如何防止用户遭受sql注入攻击?
- c# - 如何使用 jquery 在 asp.net 中的母版页中的所有操作上加载加载器
- javascript - 日期时间选择器在引导程序中不起作用
- c# - 如何使用 LINQ to sql 将数据插入到 4 个具有关系的表中
- typo3 - Typo3 9.x RTE 配置
- android - 对话框底部的添加按钮