简体   繁体   English

Selenium:每次页面加载时调用方法

[英]Selenium: Call method every time a page loads

I'm trying to throw an exception each time a certain text appears on a page. 每当页面上出现某些文本时,我都试图抛出异常。 For this to be practical, a method should check every time a page has finished loading if the exception should be thrown or not. 为了使这一点切实可行,一种方法应在每次页面完成加载时检查是否应引发异常。 The main problem I'm having right now is with clicks, AKA when a new page is opened as a result of a click on a WebElement. 我现在遇到的主要问题是单击,也就是单击WebElement导致打开新页面时的AKA。

How can one achieve that custom methods are always called when a page is finished with loading? 如何完成页面加载后始终调用自定义方法?

Here is the Answer to your Question: 这是您的问题的答案:

When we use the WebDriver through the default configuration the attribute pageLoadStrategy is set to normal which implies that once the page loading is totally complete the document.readyState is set to complete that is when WebDriver performs the next action as per your script. 当我们通过默认配置使用WebDriver时,属性pageLoadStrategy设置为normal ,这意味着一旦页面加载完全完成, document.readyState readyState就会设置为complete ,这是WebDriver根据您的脚本执行下一个操作时。

Now if you want to be convinced by yourself that the document.readyState is set to complete you can implement the following code block in your script with the help of a EplicitWait for a status log on your console: 现在,如果您想让自己确信document.readyState已设置为complete ,则可以在EplicitWait的帮助下在控制台上实现以下代码块,以在控制台上记录状态日志:

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

Let me know if this Answers your Question. 让我知道这是否回答了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM