如何使用Ghostdriver分配专用IP地址和端口

时间:2015-04-11 00:33:00

标签: ip ubuntu-14.04 ghostdriver

我写了一个 Ghostdriver Maven Java项目,我在Ubuntu 14.04 64bit中使用shell脚本调用了很多jar文件实例。

我的shell脚本中的每一行都调用了我的jar文件的新实例。

每行的格式:

screen -dmS name java -jar /path/name.jar arg1 arg2 arg3

目前我有1个网络接口(eth0)分成多个网络别名(eth0:1,eth0:2等),每个别名都指向一个私有IP,后者又指向一个公共IP。

我正在尝试找到为我的java程序的每个实例分配私有IP和可能的端口号的最佳方法。目前我在我的程序中调用Ghostdriver。

我在程序中调用ghost驱动程序的方式:

public class className {

    PhantomJSDriver driver;
    public static final File PHANTOMJS_EXE = new     File("//home/username/phantomjs/bin/phantomjs");


    public className() {
        callGhostdriver();
        driver.quit();
    }

 private void callGhostdriver() {
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setJavascriptEnabled(true);
            caps.setCapability("phantomjs.binary.path",
                    PHANTOMJS_EXE.getAbsolutePath());

            driver = new PhantomJSDriver(caps);
            driver.manage().window().maximize();
            actions = new Actions(driver);
        }
    }
}

非常感谢任何帮助。

*** ****更新

我尝试更改callGhostdriver方法,但没有成功。

private void callGhostdriver() {
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(
        PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
        PHANTOMJS_EXE.getAbsolutePath());
String[] phantomJsArgs = {"--webdriver=172.16.190.131:6781"};
dcaps.setCapability(
        PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
        phantomJsArgs);
driver = new PhantomJSDriver(dcaps);
driver.manage().window().maximize();
actions = new Actions(driver);
}
  

参数--webdriver = IP:PORT似乎被默认值覆盖   --webdriver call。

运行jar的输出:

  

* 2015年4月12日下午5:26:25 org.openqa.selenium.phantomjs.PhantomJSDriverService INFO:port:8651   2015年4月12日下午5:26:25   org.openqa.selenium.phantomjs.PhantomJSDriverService INFO:arguments:   [--webdriver = 172.16.190.131:6781, - webdriver = 8651,   --webdriver-logfile = / home / RemovedPath / phantomjsdriver.log] 2015年4月12日下午5:26:25 org.openqa.selenium.phantomjs.PhantomJSDriverService   信息:环境:{} [INFO - 2015-04-12T21:26:26.584Z] GhostDriver -   Main - 在端口8651上运行

具有不同私有IP的端口6781应该是它正在使用的,但它使用的是8651。

3 个答案:

答案 0 :(得分:2)

我能够找到一种设置端口的hackish方式,但仍然没有弄清楚如何设置私有IP,我可能必须使用iptables来路由流量。

private void loadLightWeightDriverCustom() {
    ArrayList<String> cliArgsCap = new ArrayList();
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, PHANTOMJS_EXE.getAbsolutePath());
    cliArgsCap.add("--web-security=false");
    cliArgsCap.add("--ssl-protocol=any");
    cliArgsCap.add("--ignore-ssl-errors=true");
    capabilities.setCapability(
            PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
    driver = new PhantomJSDriver(Configure(cliArgsCap), capabilities);
}

private PhantomJSDriverService Configure(ArrayList<String> cap) {
    return new PhantomJSDriverService.Builder().usingPhantomJSExecutable(PHANTOMJS_EXE)
            .usingPort(5555)
            .usingCommandLineArguments(
                    (cap.toArray(new String[cap.size()])))
            .build();
}

答案 1 :(得分:1)

试试这个 - &gt; List<String> cliArgsCap = Arrays.asList( "--webdriver=172.16.190.131:6781"); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

答案 2 :(得分:0)

我能够找到--webdriver命令的修复程序,https://github.com/detro/ghostdriver/pull/438,我编译了那个jar并将其加载。现在我得到了

Apr 24, 2015 10:35:04 AM org.openqa.selenium.phantomjs.PhantomJSDriverService 
INFO: executable: C:*Removed Path\phantomjs-2.0.0-windows\bin\phantomjs.exe
Apr 24, 2015 10:35:04 AM org.openqa.selenium.phantomjs.PhantomJSDriverService 
INFO: port: 41533
Apr 24, 2015 10:35:04 AM org.openqa.selenium.phantomjs.PhantomJSDriverService 
INFO: arguments: [--webdriver=127.0.0.1:6666, --webdriver-logfile=C:\Removed Path*\phantomjsdriver.log]
Apr 24, 2015 10:35:04 AM org.openqa.selenium.phantomjs.PhantomJSDriverService 
INFO: environment: {}
[INFO - 2015-04-24T14:35:06.222Z] GhostDriver - Main - running on port 6666
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26'
System info: host: 'MBC5708', ip: '167.74.185.13', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Driver info: driver.version: PhantomJSDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:139)

调用phantomjs的方法是:

  ArrayList<String> cliArgsCap = new ArrayList<String>();
    DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
    cliArgsCap.add("--webdriver=6666");
    capabilities.setCapability(
    PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,PHANTOMJS_EXE.getAbsolutePath());
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
    driver = new PhantomJSDriver(capabilities);