确认页面是否已加载(Confirm the Page is loaded or not)

编程入门 行业动态 更新时间:2024-10-15 12:35:09
确认页面是否已加载(Confirm the Page is loaded or not)

我在一个页面中有超链接列表,当我单击链接时,它们将重定向到新选项卡。 如何确定页面是否已加载。 我已经使用do while循环来查找元素是否启用。 但我只是“没有这样的元素”。 你能帮忙吗?下面是一段代码。 我也尝试过显式等待。 但得到同样的问题。

WebElement element7 = driver.findElement(By.id("MenuControlBNY_MenuNavBar_MenuControlBNY_MenuNavBar_p11__Tree_item_2_cell")); if (element7.isEnabled()) { element7.click(); System.out.println(" Report is selected"); } boolean element8 = false; int count = 0 ; do { element8 = driver.findElement(By.id("working")).isEnabled(); System.out.println("Report is loaded"); count = count+1; if(count == 1000) { break; } }while(element8 == true);

i have list of Hyperlinks in one page, when i click the links , they are redirecting to the new tab. how do i find out whether the page is loaded or not. i have used the do while loop for find out the element is enabled or not. but i am getting "no such element" only. could you please help on this.. Below is the piece of code. i have tried with Explicit wait also . but getting the same issue.

WebElement element7 = driver.findElement(By.id("MenuControlBNY_MenuNavBar_MenuControlBNY_MenuNavBar_p11__Tree_item_2_cell")); if (element7.isEnabled()) { element7.click(); System.out.println(" Report is selected"); } boolean element8 = false; int count = 0 ; do { element8 = driver.findElement(By.id("working")).isEnabled(); System.out.println("Report is loaded"); count = count+1; if(count == 1000) { break; } }while(element8 == true);

最满意答案

我使用以下内容,它相当自我解释。

private static WebDriverWait wait = new WebDriverWait(driver, 60); private static JavascriptExecutor js = (JavascriptExecutor) driver; public static void waitForPageLoaded() { wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { Boolean res = (js.executeScript("return document.readyState").equals("complete")); System.out.println("[DEBUG] waitForPageLoaded: " + res); return res; } }); }

编辑:当页面处于就绪状态(即页面加载)时,这将返回true。 因此,在搜索元素之前,您可以调用上述方法。

I use the following, its fairly self explanatory.

private static WebDriverWait wait = new WebDriverWait(driver, 60); private static JavascriptExecutor js = (JavascriptExecutor) driver; public static void waitForPageLoaded() { wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { Boolean res = (js.executeScript("return document.readyState").equals("complete")); System.out.println("[DEBUG] waitForPageLoaded: " + res); return res; } }); }

EDIT: This will return true when the page is in a ready state ie the page is loaded. So you would call the above method prior to searching for your element.

更多推荐

本文发布于:2023-08-07 06:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   页面   Confirm   Page   loaded

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!