Question

I was successfully able to setup androiddriver on an emulator, but am struggling to get through an httplib error. Here are the steps I took after setting up the android sdk on mac.

1.  ./adb devices, which returned emulator-5554
2. ./adb emulator-5554 -s forward tcp:8080 tcp:8080
3. visited http://localhost:8080/wd/hub/status in my browser and received a { status: 0 }
4. In the python shell (tried this on both python 2.7 & 2.6), I did:

>> from selenium import webdriver
>> from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
>> driver = webdriver.Remote("http://localhost:8080/wd/hub",     webdriver.DesiredCapabilities.ANDROID)
>> driver.get("http://www.google.com")

Some of the time, when I set the driver variable, I would get the error (below) and sometimes not. The same was true for the driver.get command.

The stacktrace is below:

  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 177, in get
    self.execute(Command.GET, {'url': url})
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 163, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 396, in _request
response = opener.open(request)
  File     "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 394, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 412, in _open
'_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1199, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1170, in do_open
    r = h.getresponse(buffering=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

Does anyone else have an idea of a solution to this?

Was it helpful?

Solution

As you say this is fixed in the newer version of the android-server. However, for those that can't update: the cause of this is due to the driver assuming the page is done loading prematurely to the actual page being loaded. Selenium receives some HTTP headers from the driver and the status line is invalid. For example,

instead of HTTP/1.0 200 the status line is blank (like above).

One way I have found to fix this is to wait a time period that is sufficiently long enough where the page is assumed to have been loaded and then attempt to establish a connection.

I usually get this exception with this line of code:

android = webdriver.Remote(command_executor='http://127.0.0.1:8080/wd/hub', desired_capabilities=capabilities.ANDROID)

So to fix, I just changed the code to wait until the call succeeds:

android = None
while android is None:
    try:
        android = webdriver.Remote(command_executor='http://127.0.0.1:8080/wd/hub', desired_capabilities=capabilities.ANDROID)
    except:
        time.sleep(1)
        pass

Note that I was still having issues with save_screenshot and BadStatusLine until I downgraded from 2.3.2 APK to 2.2.1 APK

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top