如何使用 Selenium 处理错误 'Page reload detected during async script'

How to handle the error 'Page reload detected during async script' using Selenium

如何使用 Selenium 和 InternetExplorerDriver 处理错误Page reload detected during async script

错误堆栈跟踪:

org.openqa.selenium.JavascriptException: Page reload detected during async script
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_221'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.edgechromium: false, ie.edgepath: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:24135/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: f8538187-14df-4144-8bd0-605ce398395e
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.executeAsyncScript(RemoteWebDriver.java:506)
    at stepdefinition.Steps.I_selected_the_Client(Steps.java:60)

这个错误信息...

org.openqa.selenium.JavascriptException: Page reload detected during async script 
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' 
System info: os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_221' 
Driver info: org.openqa.selenium.ie.InternetExplorerDriver

...暗示 InternetExplorerDriver 无法与 Browsing Context 交互,即 InternetExplorer Browser届会.

关于以下内容的更多详细信息:

  • Selenium 绑定艺术,即 Protractor / Java / Python / C#.
  • 客户端版本。
  • InternetExplorerDriver 版本。
  • 相关HTML(如果适用)。

会帮助我们构建一个规范的答案。

但是,如果您使用的是 Protractor as per the documentation in Page reload detected during async script,则此错误意味着:

There was a navigation or reload event while a command was pending on the browser. Usually, this is because a click action or navigation resulted in a page load. Protractor is trying to wait for Angular to become stable, but it's interrupted by the reload.

解决方案

作为解决方案,您可能需要插入一个 browser.wait 条件以确保在继续之前加载已完成。

能否请您提供 a minimal sample to reproduce 问题?您使用的是哪个代码片段 运行,错误发生在哪一行?您可以按照以下示例在 Java:

中使用 Selenium webdriver 自动化 IE
import org.openqa.selenium.By;     
import org.openqa.selenium.WebDriver;     
import org.openqa.selenium.ie.InternetExplorerDriver;     
import org.openqa.selenium.WebElement; 

public class new_java_class {              
public static void main(String[] args) {     
  //add the IE web driver path here..     
  System.setProperty("webdriver.ie.driver","your\path\to\IEwebdriver\IEDriverServer.exe");              
       WebDriver browser = new InternetExplorerDriver();     
       //replace the URL of the web page here..     
       browser.get("http://testsite.login/");                     
       WebElement username = browser.findElement(By.name("uname"));      
       username.sendKeys("test_user");                     
       WebElement password = browser.findElement(By.name("psw"));        
       password.sendKeys("abcd@1234");                     
       WebElement btn = browser.findElement(By.name("signIn"));      
       btn.click();                     
}              
} 

问题已通过使用 CSS 定位器而不是 AsynScript 在 Web 元素上执行点击操作得到解决。谢谢