Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能拿到我的电脑';s IP(Java/Selenium)?_Java_Selenium_Ip - Fatal编程技术网

我怎样才能拿到我的电脑';s IP(Java/Selenium)?

我怎样才能拿到我的电脑';s IP(Java/Selenium)?,java,selenium,ip,Java,Selenium,Ip,到目前为止,我做了以下工作: driver.get("http://www.whatismyip.com/"); String myIP = driver.findElement(By.id("greenip")).getText(); System.out.println(myIP); 还有更好的办法吗 (PS:NetworkInterface提供了多个IP,另一方面,InetAddress.getLocalHost().getHostAddress()提供了错误的IP。)问题是没有像我的I

到目前为止,我做了以下工作:

driver.get("http://www.whatismyip.com/");
String myIP = driver.findElement(By.id("greenip")).getText();
System.out.println(myIP);
还有更好的办法吗


(PS:NetworkInterface提供了多个IP,另一方面,
InetAddress.getLocalHost().getHostAddress()
提供了错误的IP。)

问题是没有像我的IP这样的东西。您的计算机可以有多个接口,每个接口可以有多个IP。所以我真的不认为你想参与其中。您所能做的就是尝试使用web API获取外部IP地址。这里有一段java代码,它实际上只是在这些站点的帮助下在internet上查找您的ip

    public static String getIpAddress() 
    { 
    URL myIP;
    try {
        myIP = new URL("http://api.externalip.net/ip/");

        BufferedReader in = new BufferedReader(
                new InputStreamReader(myIP.openStream())
                );
        return in.readLine();
    } catch (Exception e) 
    {
        try 
        {
            myIP = new URL("http://myip.dnsomatic.com/");

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(myIP.openStream())
                    );
            return in.readLine();
        } catch (Exception e1) 
        {
            try {
                myIP = new URL("http://icanhazip.com/");

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(myIP.openStream())
                        );
                return in.readLine();
            } catch (Exception e2) {
                e2.printStackTrace(); 
            }
        }
    }

return null;
}

现在,您可以从internet获取ip,但这也是您的外部ip,您可能想签出,或者您所说的更好是什么意思?更快,更可靠?最好的方法是不含硒,谢谢你的回复。我使用硒只是因为我不知道其他的方法。还有别的办法吗?我猜你对网络一无所知,也不知道你的计算机有一个不可路由的私有IP地址。获取外部IP的最好方法是询问其他人,就像你正在使用的网站一样。就这样吧!