selenium+phantomjs報錯:Unable to find a free port的分析和解決


selenium+phantomjs報錯:Unable to find a free port的分析和解決

Table of Contents

1 現象

在作項目時,發如今某臺機器上使用selenium+phantomjs時報以下錯誤:java

java.lang.RuntimeException: Unable to find a free port
        at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:67)
        at org.openqa.selenium.phantomjs.PhantomJSDriverService$Builder.build(PhantomJSDriverService.java:443)
        ...

2 分析

經過跟蹤源代碼(org.openqa.selenium.net.PortProber.createAcceptablePort),發現:sql

if (FIRST_PORT == LAST_PORT) {
        return FIRST_PORT;
}

在該服務器上,FIRSTPORT = LASTPORT = 1024,所以老是返回1024。bash

查看服務器的可用本地端口配置,以下:服務器

[gyx@interface01 ~]$ cat /proc/sys/net/ipv4/ip_local_port_range
1024	65535

由於這臺機器的最低可用端口配置成了1024,而其餘機器都比這個大不少,所以形成了上述問題。dom

3 解決辦法

由於該服務器還有別的用處,不能隨意修改可用端口配置,因此,暫時經過修改createAcceptablePort中相應代碼解決問題。以下:ide

if (FIRST_PORT == LAST_PORT) {
//                return FIRST_PORT;
        final int randomInt = random.nextInt();
        System.out.println("randomInt = " + randomInt);
        final int portWithoutOffset = Math.abs(randomInt % (HIGHEST_PORT - START_OF_USER_PORTS + 1));
        return portWithoutOffset + FIRST_PORT;
}

Author: galaxypost

Created: 2016-07-28 Thu 09:58ui

Emacs 24.5.6 (Org mode 8.2.10)spa

Validate.net

相關文章
相關標籤/搜索