gpt4 book ai didi

java - Selenium HtmlUnitDriver 程序中捕获的 SocketException

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:40 25 4
gpt4 key购买 nike

我编写了一个小脚本,它采用我公司收到的爱普生打印机的默认 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();行。

最佳答案

你试过打电话吗

driver.quit();

之前

reset.submit();

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

希望对你有帮助。

关于java - Selenium HtmlUnitDriver 程序中捕获的 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40404654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com