0

I keep getting an error like this

org.openqa.selenium.WebDriverException: java.io.IOException: unexpected end of stream on Connection{localhost:13080, proxy=DIRECT hostAddress=localhost/127.0.0.1:13080 cipherSuite=none protocol=http/1.1}

when trying to read cookies from firefox. The exact same action can be performed with chrome with no problem. Port used is pretty random, anytime I start a new test I get a new port assigned as well. Should specifying another port be able to do something here? The line of code that produces it is:

driver.manage().getCookies().forEach(cookie -> System.out.println(cookie.toString()));

I wonder if it's an initialization problem with firefox and setting some value will get me past it. The initialization I do is just this:

FirefoxOptions Foptions = new FirefoxOptions();
Foptions.setBinary("/home/user/firefox/firefox");
Foptions.setCapability("marionette", true);
driver =  new FirefoxDriver(Foptions);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
gmitsios
  • 21
  • 1
  • 5

2 Answers2

0

It is tough to analyze the error without the error stack trace. The error stack trace would have helped immencely to debug the issue.

However this error message...

org.openqa.selenium.WebDriverException: java.io.IOException: unexpected end of stream on Connection{localhost:13080, proxy=DIRECT hostAddress=localhost/127.0.0.1:13080 cipherSuite=none protocol=http/1.1}

...implies that the java.io.IOException was raised when you tried to invoke getCookies().

Your main issue can be one of the following:

  • Reason A: When you invoked driver.manage().getCookies() though apparently it seems the HTML DOM have loaded but still some javaScript/Ajax Calls are active setting the cookies.
  • Solution: Induce WebDriverWait for an element to be clickable as follows:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btn_download"))).click()
    
  • Reason B: As per java.io.IOException: unexpected end of stream on Connection in android it may be possible that the server throwed an error and shut down as the parsing of the request was in progress.

  • Solution: Cross check if you are able to access the elements within the webpage without mingling with the cookies.
  • Reason C: Another reason can be the port being used by geckodriver/marionette is being used by some other application/service.
  • Solution: Freeup the ports used by GeckoDriver/Marionette
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey thanks for the answer. Reason A is definitely not the problem. Double checked with wait and through javascript (boolean)js.executeScript("return window.jQuery != undefined && jQuery.active==0");. Reason B is also not applied. I can grab any element from page no problem. About Reason C now. Ports are being randomly assigned as I start the test suite. Any suggestion on which port to choose and set as preference? – gmitsios Jun 26 '18 at 12:48
  • I also tried with Foptions.addPreference("network.cookie.cookieBehavior", 0);, where "0" is for enabling all cookies by default but no luck either. – gmitsios Jun 26 '18 at 12:49
  • Unable to find any docs of geckodriver port usage, but somewhere it is documentated _Marionette will listen on port 2828_ – undetected Selenium Jun 26 '18 at 13:01
  • Thanks, I'll try it. I currently start FirefoxDriver with FirefoxOptions but I need FirefoxProfile to set desired port. Any idea on how I can cast a FirefoxProfile value to FirefoxOptions value? I know I can do the opposite with getProfile but haven't found any constructor with FirefoxProfile input. – gmitsios Jun 26 '18 at 14:05
0

I was getting the same issue while using the driver.manage().getCookies() method on selenium 3.12.0 and above, downgraded my selenium version to 3.11.0 and it worked fine, The issue is introduced in the latest version.

  • Sounds very plausible. I am going to have to deal with it again in a few days so i will let you know if this was the problem or not. Thanks – gmitsios Jul 16 '18 at 13:16