在Selenium HtmlUnitDriver程序中捕获了SocketException

时间:2016-11-03 14:53:01

标签: java selenium automation

我编写了一个小脚本,它接受我们公司收到的Epson打印机的默认IP地址,并根据要求自动更改。这是使用Selenium HtmlUnitDriver完成的。

脚本获取页面,插入新IP,然后提交它。因为一旦我们再次提交IP就会更改,页面不再是192.168.192.168,并且脚本不想完成。

以下是剧本:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Main {
    public static void main(String[] args) {

        // Creating a new instance of the HTML unit driver.
        WebDriver driver = new HtmlUnitDriver();
        driver.get("http://192.168.192.168/ctcpip.htm");

        // Find and change the IP Address field.
        WebElement element = driver.findElement(By.name("IpAddress"));
        element.clear();
        element.sendKeys("192.168.192.169");
        element.submit();

        // Reset the printer. This changes it's IP as well, causing the initial driver page to no longer exist.
        WebElement reset = driver.findElement(By.name("Submit"));
        reset.submit();

        // The script never gets this far.
        driver.quit();

    }
}

脚本在完成之前会超时。单击reset元素后,http://192.168.192.168/ctcpip.htm的初始URL实际上不存在,因为我们已将其更改为192.169.192.169。这是预期的行为,也是整个计划的重点。

控制台显示:

Nov 03, 2016 10:36:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://192.168.192.168:80: Operation timed out
Nov 03, 2016 10:36:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://192.168.192.168:80
Exception in thread "main" java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to 192.168.192.168:80 [/192.168.192.168] failed: Operation timed out
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:739)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:820)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1325)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1268)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1216)
    at org.openqa.selenium.htmlunit.HtmlUnitWebElement.submit(HtmlUnitWebElement.java:175)
    at Main.main(Main.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 192.168.192.168:80 [/192.168.192.168] failed: Operation timed out
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:178)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1313)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1230)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
    at com.gargoylesoftware.htmlunit.WaitingRefreshHandler.handleRefresh(WaitingRefreshHandler.java:92)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeRefreshIfNeeded(HtmlPage.java:1446)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:306)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
    at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2074)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:733)
    ... 11 more
Caused by: java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    ... 29 more

Process finished with exit code 1

如何告诉我的驱动程序实例页面已经变得非常酷,以便进程可以正常退出?

脚本需要才能到达driver.quit();行。

3 个答案:

答案 0 :(得分:1)

如果某些东西在Selenium中不能使用findelement,那么每个人都使用js :)。 怎么样?

 driver.executeScript("window.document.getElementsByName("Submit")[0].click()");

指数错误,需要调整

答案 1 :(得分:1)

请尝试使用以下代码:

如果提供的主机或主机IP地址在网络中不可用,它将退出驱动程序。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Main {
    public static void main(String[] args) {

    // Creating a new instance of the HTML unit driver.
        WebDriver driver = new HtmlUnitDriver();
        try{
            driver.get("http://192.168.192.168/ctcpip.htm");
            WebElement element = driver.findElement(By.name("IpAddress"));
            element.clear();
            element.sendKeys("192.168.192.169");
            element.submit();

        // Reset the printer. This changes it's IP as well, causing the   initial driver page to no longer exist.
            WebElement reset = driver.findElement(By.name("Submit"));
            reset.submit();
        }
        catch(HttpHostConnectException e){
            System.out.println("Host Not Found : "+ e.getMessage())
        }

        finally{
        driver.quit();
        }
    }
}

答案 2 :(得分:1)

你试过打电话吗

driver.quit();

之前

reset.submit();

?您不再使用驱动程序来检索任何元素,因此您可以在提交页面之前退出。

我希望它有所帮助。