最近我得到了一个新的虚拟服务器,我试图配置,因为有些日子让我的基于Java(8)的selenium web应用程序运行。这个webapp在tomcat(8.5.31)中运行,并使用selenium进行一些基本的网站抓取。服务器的操作系统是gentoo(在我有一个运行良好的debian系统之前)
我已经花了几天时间才能让它发挥作用,但现在我仍然坚持以下错误:
btn.isSelected = !btn.isSelected
这是流程列表显示的内容:
1526203737341 geckodriver INFO geckodriver 0.20.1
1526203737367 geckodriver INFO Listening on 127.0.0.1:25016
1526203737796 mozrunner::runner INFO Running command: "/usr/bin/firefox-bin" "-marionette" "-profile" "/tmp/rust_mozprofile.ESuH8rdqpwT8"
1526203737802 geckodriver::marionette DEBUG Waiting 60s to connect to browser
1526203739872 Marionette INFO Listening on port 2828
1526203797867 webdriver::server DEBUG <- 500 Internal Server Error {"value":{"error":"unknown error","message":"connection refused","stacktrace":""}}
11:29:57.944 [pool-2-thread-1] ERROR test.Test - Exception occured while processing.
org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'gentoo', ip: 'xxx.xxx.xxx.xxx', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.95-gentoo', java.version: '1.8.0_172'
Driver info: driver.version: FirefoxDriver
用于创建Firefox实例的代码(使用更详细的日志级别来获取有关该问题的更多信息):
root 7808 1 0 14:36 ? 00:00:00 /usr/bin/Xvfb :99 -screen 0 1600x1200x24 -auth /etc/conf.d/Xvfb.cfg -listen tcp
tomcat 7863 1 59 14:38 ? 00:00:51 /etc/java-config-2/current-system-vm/bin/java -Djava.util.logging.config.file=/opt/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderL
tomcat 7923 7863 0 14:38 ? 00:00:00 /opt/geckodriver --port=31399 -b /usr/bin/firefox-bin
tomcat 7943 7923 6 14:38 ? 00:00:04 /opt/firefox/firefox -marionette --headless -profile /tmp/rust_mozprofile.qpQfCj8DR1ab
tomcat 8032 7943 0 14:38 ? 00:00:00 /opt/firefox/plugin-container -greomni /opt/firefox/omni.ja -appomni /opt/firefox/browser/omni.ja -appdir /opt/firefox/browser 7943 true tab
tomcat 7977 1 0 14:38 ? 00:00:00 dbus-launch --autolaunch 32b951aa6b930bec1f195b765af588f5 --binary-syntax --close-stderr
tomcat 7978 1 0 14:38 ? 00:00:00 /usr/bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
tomcat 7993 1 0 14:38 ? 00:00:00 /usr/libexec/gconfd-2
geckodriver位于/ opt /并且可执行。
我当前的设置/配置
Selenium
所有组件都有3.12.0版本(selenium-java,selenium-remote-driver,selenium-firefox-driver,selenium-api)
Firefox
版本60.0
geckodriver
版本20.1
Xvfb配置
System.setProperty("webdriver.gecko.driver", driverGeckoPath);
LoggingPreferences loggingPrefs = new LoggingPreferences();
loggingPrefs.enable(LogType.BROWSER, Level.ALL);
loggingPrefs.enable(LogType.CLIENT, Level.ALL);
loggingPrefs.enable(LogType.DRIVER, Level.ALL);
loggingPrefs.enable(LogType.PERFORMANCE, Level.ALL);
loggingPrefs.enable(LogType.PROFILER, Level.ALL);
loggingPrefs.enable(LogType.SERVER, Level.ALL);
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("marionette", true);
desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPrefs);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
driver = new FirefoxDriver(options);
Xvfb config
XVFB=/usr/bin/Xvfb
XVFBARGS=":99 -screen 0 1600x1200x24 -auth /etc/conf.d/Xvfb.cfg -nolisten tcp"
PIDFILE=/var/run/Xvfb.pid
Xvfb init.d-script
localhost
导出显示变量(在所有用户的启动时初始化):
start() {
ebegin "Starting virtual X frame buffer: Xvfb"
start-stop-daemon --start --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS
eend ${?}
}
stop() {
ebegin "Stopping virtual X frame buffer: Xvfb"
start-stop-daemon --stop --pidfile $PIDFILE
eend ${?}
}
/etc/conf.d/hostname
DISPLAY=:99
/ etc / hosts
gentoo
非常感谢有关如何调试/解决此问题的更多信息的任何提示/技巧......
我尝试使用 127.0.0.1 gentoo
127.0.0.1 localhost
::1 localhost
运行我的selenium组件,其中有趣的是。代码:
ChromeDriver
日志显示:
System.setProperty("webdriver.chrome.driver", "/opt/chromedriver");
driver = new ChromeDriver();
当我在我的本地环境中运行应用程序时(类似配置/ gentoo),一切正常。但我不是在这里使用Xvfb而是使用xorg服务器。 这是日志记录输出:
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 24646
Only local connections are allowed.
对我来说,这似乎是木偶和geckodriver之间的连接问题。这可能是港口问题吗?在服务器上,Marionette似乎总是在端口2828上听,在我的本地机器上它似乎总是一个随机的端口......
答案 0 :(得分:0)
此错误消息......
1526203737802 geckodriver::marionette DEBUG Waiting 60s to connect to browser
1526203739872 Marionette INFO Listening on port 2828
1526203797867 webdriver::server DEBUG <- 500 Internal Server Error {"value":{"error":"unknown error","message":"connection refused","stacktrace":""}}
11:29:57.944 [pool-2-thread-1] ERROR test.test.test.test.test.Test - Exception occured while processing.
org.openqa.selenium.WebDriverException: connection refused
...意味着GeckoDriver无法成功启动/生成新的Web客户端会话/实例。
您的主要问题是您已将DesiredCapabilities
类型的对象(即 desiredCapabilities )分配给FirefoxOptions
即选项的方式。正确的方法是将DesiredCapabilities
类对象,即 desiredCapabilities 通过merge()
类中的MutableCapabilities
函数传递到 FirefoxOptions 类对象如下:
System.setProperty("webdriver.gecko.driver", driverGeckoPath);
LoggingPreferences loggingPrefs = new LoggingPreferences();
loggingPrefs.enable(LogType.BROWSER, Level.ALL);
loggingPrefs.enable(LogType.CLIENT, Level.ALL);
loggingPrefs.enable(LogType.DRIVER, Level.ALL);
loggingPrefs.enable(LogType.PERFORMANCE, Level.ALL);
loggingPrefs.enable(LogType.PROFILER, Level.ALL);
loggingPrefs.enable(LogType.SERVER, Level.ALL);
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("marionette", true);
desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPrefs);
FirefoxOptions options = new FirefoxOptions();
options.merge(desiredCapabilities);
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
driver = new FirefoxDriver(options);
由于您仍然面临同样的错误,请考虑执行以下步骤:
Test
。driver.quit()
方法中调用tearDown(){}
以关闭&amp;正常销毁 WebDriver 和 Web客户端实例。答案 1 :(得分:0)
对于那些可能会担心的人,我遇到了同样的问题,并陷入了“拒绝连接”的节日。经过一整夜的尝试和错误,我发现之前的geckodriver,Selenium和firefox进程没有关闭,并且大约有50个打开进程。这三个简单的命令解决了我的问题
pkill geckodriver
pkill firefox
pkill selenium