簡體   English   中英

org.openqa.selenium.WebDriverException:[異常…“組件未初始化”錯誤使用 GeckoDriver 和 Tor 瀏覽器與 Selenium ZD52387880E1EA22817A7Z2D3759213891

[英]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 版本? I am asking because there is an issue with the firefox driver version < 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]

...暗示木偶在嘗試與所需元素交互時出錯。


即使在DOM 樹完全渲染之前, get window似乎也被調用得太早了。 更具體地說, addEventListener甚至在瀏覽器客戶端(即 Web 瀏覽器)達到'document.readyState'等於"complete"之前就被調用了。 通常,一旦滿足此條件, Selenium 就會執行下一行代碼。


解決方案

一個快速的解決方案是presenceOfElementLocated您嘗試與新加載的網頁上的任何元素而不是ExpectedConditions交互之前,您需要為elementToBeClickable()誘導WebDriverWait並且您可以使用以下任一定位器策略

  • 使用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");

其他注意事項

確保這件事:

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

  • Selenium升級到當前級別版本 3.141.59
  • GeckoDriver升級到GeckoDriver v0.26.0級別。
  • Firefox升級到當前 _Firefox v77.0.1 _ 級別。
  • 確保您使用的二進制文件的版本是兼容的。

您可以在給定 Geckodriver 版本支持哪些 Firefox 瀏覽器版本中找到詳細討論?

  • GeckoDriver 出現在所需位置。
  • GeckoDriver 對非 root用戶具有可執行權限。
  • 通過您的IDE清理您的項目工作區,並僅使用所需的依賴項重建您的項目。
  • 如果您的基礎Web 客戶端版本太舊,請卸載它並安裝最新的 GA 和發布版本的Web 客戶端
  • 重新啟動系統
  • 以非 root 用戶身份執行您的Test
  • 始終在tearDown(){}方法中調用driver.quit()以優雅地關閉和銷毀WebDriverWeb 客戶端實例。

參考

您可以在以下位置找到一些相關的討論:


奧特羅

將 window 切換到底部擴展塢時發生“NS_ERROR_NOT_INITIALIZED”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM