2017-05-13 45 views
2

我有這個錯誤加載一個URL,並得到一個奇怪的錯誤我不知道如何解決。我的情況要求我必須使用PhantomJS,因爲我不認爲我可以在AWS Lambda上使用Firefox驅動程序,在我的筆記中,我遇到了Chromedriver無法點擊的按鈕。httplib.BadStatusLine:''與Selenium和PhantomJS

如果我爲Chrome或Firefox切換PhantomJS,則url解析正常。

使用硒== 3.4.1和2.1.1 PhantomJS

user_agent = (
     "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0") 

dcap = dict(DesiredCapabilities.PHANTOMJS) 
dcap["phantomjs.page.settings.userAgent"] = user_agent 
dcap["phantomjs.page.settings.javascriptEnabled"] = True 

browser = webdriver.PhantomJS(service_log_path=os.path.devnull, service_args=[ 
    '--ignore-ssl-errors=true'], desired_capabilities=dcap) 

browser.set_window_size(1120, 550) 
browser.get('https://drizly.com/session/new') 



File "main.py", line 257, in <module> 
    lambda_handler(None, None) 
    File "main.py", line 103, in lambda_handler 
    browser.get('https://drizly.com/session/new') 
    File "/Users/aymon/Envs/drizly/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 264, in get 
    self.execute(Command.GET, {'url': url}) 
    File "/Users/aymon/Envs/drizly/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in execute 
    response = self.command_executor.execute(driver_command, params) 
    File "/Users/aymon/Envs/drizly/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 464, in execute 
    return self._request(command_info[0], url, body=data) 
    File "/Users/aymon/Envs/drizly/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 526, in _request 
    resp = opener.open(request, timeout=self._timeout) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open 
    response = self._open(req, data) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open 
    '_open', req) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain 
    result = func(*args) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1227, 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 1200, in do_open 
    r = h.getresponse(buffering=True) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1132, in getresponse 
    response.begin() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 453, in begin 
    version, status, reason = self._read_status() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 417, in _read_status 
    raise BadStatusLine(line) 
httplib.BadStatusLine: '' 

回答

0

BadStatusLineHTTPException一個子類,其如果a server responds with a HTTP status code that we don’t understand上升。你可能想抓住它,如下

#... 
browser.set_window_size(1120, 550) 
try: 
    browser.get('https://drizly.com/session/new') 
except httplib.BadStatusLine as bsl: 
    print('[!!!] {message}'.format(bsl.message)) 
#... 

需要注意的是一個很好的做法是錯誤should never pass silently。因此使用print

+0

@AymonFournier它工作嗎? – Kanak

+0

其他任何驅動程序都沒有得到200 –

+0

@AmonmonFournier之外的任何其他驅動程序。許多web開發人員構建特定策略來檢測類似幻像(無頭)瀏覽器。例如。請參閱[檢測基於PhantomJS的訪問者](https://blog.shapesecurity.com/2015/01/22/detecting-phantomjs-based-visitors/)。我認爲這只是你正在經歷的。這就是爲什麼你應該嘗試超越'BadStatusLine'異常,並看看你是否真的收到網頁源。 – Kanak

相關問題