在CentOS7上设置Selenium [英] Set up Selenium on CentOS7

查看:111
本文介绍了在CentOS7上设置Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CentOS-VM上设置测试环境。

I am trying to set up test environment on my CentOS-VM.

我在Eclipse中运行的Java-maven-project中有以下三个文件:

I have the following three files in my Java-maven-project running in Eclipse:

main.java

package main;

public class main {

    public static void main(String[] args) {
        StartFirefox fire = new StartFirefox();
        StartChrome chrome = new StartChrome();

        chrome.start();
        fire.start();
    }
}

启动Firefox.java

package main;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class StartFirefox {

WebDriver fire;

    public StartFirefox() {
        System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver/geckodriver");
        System.out.println("debug 1");
        fire = new FirefoxDriver();
        System.out.println("debug 2");
        fire.manage().window().maximize();
    }

    public void start() {
        System.out.println("debug 3");
        fire.get("https://www.google.com/");
        System.out.println("debug 4");
    }
}

StartChrome.java

package main;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class StartChrome {

    WebDriver chrome;

    public StartChrome() {
        String pathToChromeDriver = "src/main/resources/chromedriver/chromedriver";
        System.setProperty("webdriver.chrome.driver", pathToChromeDriver);
        System.out.println("debug 1");
        chrome = new ChromeDriver();
        System.out.println("debug 2");
    }

    public void start() {
        System.out.println("debug 3");
        chrome.get("https://www.google.com/");
        System.out.println("debug 4");
    }

}

使用Firefox运行程序时,窗口打开,但不会转到我的网址。
相反,一段时间后(似乎是超时),我在控制台中得到以下输出:

When running the program with Firefox the window opens, but it doesn't go to my url. Instead I get the following output in the console after some time (seems to be a timeout):

debug 1
1516891060262   geckodriver INFO    geckodriver 0.19.1
1516891060265   geckodriver INFO    Listening on 127.0.0.1:32675
1516891060856   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.v4slcGFX6wrp"
1516891061783   Marionette  INFO    Listening on port 2828
Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'localhost.localdomain', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693.11.6.el7.x86_64', java.version: '1.8.0_152'
Driver info: driver.version: FirefoxDriver
remote stacktrace: stack backtrace:
   0:           0x4edb3c - backtrace::backtrace::trace::hc4bd56a2f176de7e
   1:           0x4edb72 - backtrace::capture::Backtrace::new::he3b2a15d39027c46
   2:           0x440ac8 - webdriver::error::WebDriverError::new::ha0fbd6d1a1131b43
   3:           0x447ba9 - geckodriver::marionette::MarionetteHandler::create_connection::hf0532ddb9e159684
   4:           0x428570 - <webdriver::server::Dispatcher<T, U>>::run::h2119c674d7b88193
   5:           0x4029b9 - std::sys_common::backtrace::__rust_begin_short_backtrace::h21d98a9ff86d4c25
   6:           0x40be65 - std::panicking::try::do_call::h5cff0c9b18cfdbba
   7:           0x5e6a6c - panic_unwind::__rust_maybe_catch_panic
                        at /checkout/src/libpanic_unwind/lib.rs:99
   8:           0x41eb22 - <F as alloc::boxed::FnBox<A>>::call_box::h413eb1d9d9f1c473
   9:           0x5df13b - alloc::boxed::{{impl}}::call_once<(),()>
                        at /checkout/src/liballoc/boxed.rs:692
                         - std::sys_common::thread::start_thread
                        at /checkout/src/libstd/sys_common/thread.rs:21
                         - std::sys::imp::thread::{{impl}}::new::thread_start
                        at /checkout/src/libstd/sys/unix/thread.rs:84
    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.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98)
    at main.StartFirefox.<init>(StartFirefox.java:12)
    at main.main.main(main.java:6)

我是卡在这里,因为我不知道如何解决此问题。可能是什么原因?

I am stuck here as I don't know how to fix this problem. What may be the cause?

使用Chrome运行该程序时,窗口会打开,但不会转到我的网址。
相反,我在控制台中得到以下输出:

When running the program with Chrome the window opens, but it doesn't go to my url. Instead I get the following output in the console:

debug 1
Starting ChromeDriver (v2.9.248304) on port 5186
[0.899][WARNING]: PAC support disabled because there is no system implementation
Jan 25, 2018 3:40:23 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
debug 2
debug 3
Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:5186 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'localhost.localdomain', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-693.11.6.el7.x86_64', java.version: '1.8.0_152'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
    at main.StartChrome.start(StartChrome.java:18)
    at main.main.main(main.java:9)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:5186 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:156)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:86)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    ... 17 more

何时在Windows 10上运行此项目,一切正常。

When running this project on Windows 10 it all works fine.

所以操作系统似乎有问题。但是搜索时找不到任何解决方案。它只说了有关服务器和无头版本的内容。

So it seems to be a problem with the OS. But I can't find any solutions when searching. It only says something about a server and headless version.

更新:

添加了调试消息。 Firefox仅到达消息1,而Chrome直到消息3才打印。

Added debug messages. Firefox reaches only message 1, while Chrome prints until message 3.

推荐答案

您从geckodriver 0.19.1开始的第一个错误堆栈跟踪给我们一些提示:

Your first error stack trace from the geckodriver 0.19.1 does gives us some hint:


org.openqa.selenium.WebDriverException:连接被拒绝

org.openqa.selenium.WebDriverException: connection refused

但是来自ChromeDriver(v2.9.248304)的第二个堆栈跟踪说明了一切:

But the second stack trace from ChromeDriver (v2.9.248304) says it all:


org。 openqa.selenium.WebDriverException:org.apache.http.conn.HttpHostConnectException:连接到localhost:8004 [localhost / 127.0.0.1,localhost / 0:0:0:0:0:0:0:0:1]失败:连接被拒绝(拒绝连接)

org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8004 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

/ etc / hosts文件中的条目未正确配置。

The entries within the /etc/hosts file are not properly configured.

要解决此问题,请确保Linux Box上的 / etc / hosts 包含以下条目:

To fix this, ensure that /etc/hosts on your Linux Box contains the following entry:

127.0.0.1               localhost.localdomain localhost

这篇关于在CentOS7上设置Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆