org.openqa.selenium.WebDriverException:[Exception ...“未初始化组件".在Selenium Java中使用GeckoDriver和Tor浏览器时出错 [英] org.openqa.selenium.WebDriverException: [Exception... "Component not initialized" error using GeckoDriver and Tor browser with Selenium Java

查看:134
本文介绍了org.openqa.selenium.WebDriverException:[Exception ...“未初始化组件".在Selenium Java中使用GeckoDriver和Tor浏览器时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to run Selenium with Tor Browser but get an error. When i start my code, the Tor Browser opens and calls the url https://www.trash-mail.com/adresse-erstellen/ correctly but instead of making the last command with "sendKeys" this error occurres:

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");

    }
}

如果有人可以帮助我解决我的问题,那就太好了.

推荐答案

此错误消息...

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]

......表示牵线木偶在尝试与所需元素进行交互时抛出错误.

...implies that that the Marionette threw an error while attempting to interacting with the desired element.

get window似乎早在完全渲染 DOM树之前就被调用了.更具体地说,甚至在 Browser Client (即Web浏览器)达到等于"complete"'document.readyState'之前,调用addEventListener.通常,一旦满足此条件, Selenium 执行下一个代码行.

It seems the get window was invoked too early even before the 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 Selenium performs the next line of code.

一种快速的解决方案是,在尝试与新加载的网页上的任何元素进行交互之前,而不是与 WebDriverWait 用于elementToBeClickable(),并且您可以使用以下任一定位器策略:

A quick solution will be to before you try to interact with any of the element on a fresh loaded webpage instead of ExpectedConditions as presenceOfElementLocated you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

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

  • 确保:

    • Upgrade JDK to recent levels JDK 8u251.

    注意: Selenium 有问题Java 9 Java 13 .

    • 升级到当前级别> 版本3.141.59 > .
    • GeckoDriver 升级到 GeckoDriver v0.26.0 级.
    • Firefox 已升级到当前的_Firefox v77.0.1 _级别.
    • 确保所使用的二进制文件的版本兼容.
      • Upgrade Selenium to current levels Version 3.141.59.
      • Upgrade GeckoDriver to GeckoDriver v0.26.0 level.
      • Firefox is upgraded to current _Firefox v77.0.1 _ levels.
      • Ensure that the version of the binaries you are using are compatable.
      • 您可以在中找到详细的讨论.给定的Geckodriver版本支持哪些Firefox浏览器版本?

        • GeckoDriver存在于所需位置.
        • GeckoDriver对非root用户用户具有可执行权限.
        • 通过 IDE
        • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项.
        • 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA.
        • 进行系统重启.
        • 以非root用户身份执行Test.
        • 始终在tearDown(){}方法中调用driver.quit()以关闭&优雅地销毁 WebDriver Web Client 实例.
    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆