SocketException error in Selenium HtmlUnitDriver program

I wrote a small script that accepts the default IP address for the Epson printers we get from my company and automatically changes them as required. This is done using Selenium HtmlUnitDriver.

The script receives the page, inserts the new IP address and sends it. Since the IP changes after sending a second time, the page is no longer 192.168.192.168 , and the script does not want to end.

Below is the script:

 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 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(); } } 

The script will expire before it can complete. When the reset element is clicked, the starting URL http://192.168.192.168/ctcpip.htm actually missing, since we changed it to 192.169.192.169 . This is the expected behavior and the whole point of the program.

The console reads:

 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 

How do I tell my driver instance that it will completely cool the page so that the process can exit normally?

a script is required to reach the line driver.quit(); .

+5
source share
3 answers

Did you try to call

 driver.quit(); 

before

 reset.submit(); 

? You no longer use the driver to retrieve any item, so you can exit before submitting the page.

Hope this helps.

+1
source

If something does not work with findelement in Selenium, everyone uses js :). How about this?

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

The index may be incorrect and needs to be adjusted.

+1
source

Please try with the following code:

It will exit the driver if the provided IP address of the host or host is not available on the network.


 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 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(); } } } 
+1
source

Source: https://habr.com/ru/post/1259227/


All Articles