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

相关文章
相关标签/搜索