org.openqa.selenium.WebDriverException:[异常..."Component not initialized" 使用带有 Selenium 的 GeckoDriver 和 Tor 浏览器时出错 Java

org.openqa.selenium.WebDriverException: [Exception... "Component not initialized" error using GeckoDriver and Tor browser with Selenium Java

我尝试使用 Tor 浏览器 运行 Selenium,但出现错误。 当我开始我的代码时,Tor 浏览器打开并正确调用 url https://www.trash-mail.com/adresse-erstellen/ 但不是使用“sendKeys”发出最后一个命令,而是出现此错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: [Exception... "Component not initialized"  nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)"  location: "JS frame :: chrome://marionette/content/modal.js :: get window :: line 143"  data: no]
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'MacBook-Pro.local', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '14.0.1'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 68.9.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:buildID: 20200407010101, moz:geckodriverVersion: 0.25.0, moz:headless: false, moz:processID: 21229, moz:profile: /var/folders/5s/9gmx38s53zl..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 18.7.0, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: d12a4cf5-9c6f-1549-9dc4-c272dcc7aaee
*** Element info: {Using=css selector, value=#form-password-new1}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    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.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:420)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:431)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at com.eviltester.webdriver.Connection.open(Connection:28)

到目前为止,这是我的代码:

package com.eviltester.webdriver;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Connection {

    private static final String macPath = "/Users/Admin/Documents/Projekte/workspace/project1/webdriver/geckodriver";
    private static final String torPath = "/Users/Admin/Documents/Projekte/workspace/project1/webdriver/tor";
    private static final String torFirefox = "/Applications/Tor Browser.app/Contents/MacOS/firefox";
    private static final String torProfile ="/Applications/Tor Browser.app/Contents/Resources/TorBrowser/Tor";
    
    private FirefoxOptions options = new FirefoxOptions();
    
    public Connection() {
        System.setProperty("webdriver.gecko.driver", macPath);
    }
    
    public void open() {
        FirefoxProfile profile = new FirefoxProfile(new File(torProfile));
        FirefoxBinary binary = new FirefoxBinary(new File(torFirefox));
        
        options.setBinary(binary);
        options.setProfile(profile);
        FirefoxDriver driver = new FirefoxDriver(options);
        
        driver.navigate().to("https://www.trash-mail.com/adresse-erstellen/");
        TimeUnit.SECONDS.sleep(5);
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#form-postbox-new"))).sendKeys("testmail123");

    }
}

如果有人能帮我解决我的问题就太好了。

您使用哪个 Geckodriver 版本?我问是因为 firefox 驱动程序版本 < 69: https://bugzilla.mozilla.org/show_bug.cgi?id=1477977, https://github.com/mozilla/geckodriver/issues/1690 有问题。我有同样的错误,但它发生在我尝试与模式 windows 交互时,而不是在调用 sendKeys 时发生。

如果可以,请尝试更新驱动版本,看看问题是否还存在。

这个错误信息...

Exception in thread "main" org.openqa.selenium.WebDriverException: [Exception... "Component not initialized"  nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)"  location: "JS frame :: chrome://marionette/content/modal.js :: get window :: line 143"  data: no]

...暗示 Marionette 在尝试与所需元素交互时抛出错误。


似乎 get window 甚至在 DOM Tree was completely rendered. To be more specific addEventListener was invoked even before the Browser Client (i.e. the Web Browser) have attained 'document.readyState' equal to "complete". Generally once this condition is fulfilled 执行下一行代码之前就被调用得太早了。


解决方案

一个快速的解决方案是在您尝试与新加载的网页而不是 ExpectedConditions as presenceOfElementLocated you need to induce for the elementToBeClickable() and you can use either of the following 上的任何元素交互之前:

  • 使用 id:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("form-postbox-new"))).sendKeys("testmail123");
    
  • 使用 cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#form-postbox-new"))).sendKeys("testmail123");
    
  • 使用 xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='form-postbox-new']"))).sendKeys("testmail123");
    

其他注意事项

确保:

  • JDK 升级到最近的水平 JDK 8u251

Note: have issues with Java 9, and Java 13.

  • Selenium 升级到当前水平 Version 3.141.59
  • GeckoDriver 升级到 GeckoDriver v0.26.0 级别。
  • Firefox 已升级到当前的 _Firefox v77.0.1 _ 级别。
  • 确保您使用的二进制文件的版本兼容。

You can find a detailed discussion in Which Firefox browser versions supported for given Geckodriver version?

  • GeckoDriver 出现在所需位置。
  • GeckoDriver 拥有 非 root 用户的可执行权限。
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • 如果您的基础 Web Client 版本太旧,则卸载它并安装最新的 GA 和发布版本的 Web Client
  • 系统重启
  • 以非 root 用户身份执行 Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例.

参考资料

您可以在以下位置找到一些相关讨论:

  • “Component not initialized” nsresult: “0xc1f30001 (NS_ERROR_NOT_INITIALIZED)” error with Selenium GeckoDriver and Mozilla

结尾

Occur the 'NS_ERROR_NOT_INITIALIZED' when switching the window to bottom dock.