im trying to execute some selenium with unittest scripts but i get the following error
Starting at: "Sat Dec 07 14:43:17 2013"
E
======================================================================
ERROR: test_template (__main__.ManageTemplates)
----------------------------------------------------------------------
Traceback (most recent call last):
File "template.py", line 70, in tearDown
self.driver.quit()
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 66, in quit
RemoteWebDriver.quit(self)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 454, in quit
self.execute(Command.QUIT)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 162, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 350, in execute
return self._request(url, method=command_info[0], data=data)
File "C:\Program Files (x86)\Python27\lib\site-packages\selenium-2.38.1-py2.7.egg\selenium\webdriver\remote\remote_connection.py", line 381, in _request
self._conn.request(method, parsed_url.path, data, headers)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 1001, in _send_request
self.putrequest(method, url, **skips)
File "C:\Program Files (x86)\Python27\lib\httplib.py", line 871, in putrequest
raise CannotSendRequest()
CannotSendRequest
----------------------------------------------------------------------
Ran 1 test in 766.686s
FAILED (errors=1)
the errors accure in template.py at line 70 in tearDown() function, which is a default unittest function autogenerated with Selenium-IDE
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
EDIT:
This Problem happened when i updated Firefox to 26, even when i updated to selenium 2.39.0 the problem didnt go away
Source:
import unittest
from os import path
from config import config
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from PyWebBotClass import PyWebBot
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from os import listdir, environ
from os.path import isfile, join
import time
class ManageReceivers(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
self.base_url = config['baseurl']
self.verificationErrors = []
self.accept_next_alert = True
def setup_bot(self, f):
self.bot = PyWebBot(self.driver, path.join(config['configs'],f))
self.bot.set_LogPath(config['LogPath'])
self.bot.set_ScreenshotPath(config['screenshots'])
self.bot.set_ConfigBaseURL(config['baseurl'])
def test_receiver_profile(self):
self.imported = False
for f in listdir(config['configs']):
if isfile(join(config['configs'],f)):
self.setup_bot(f)
if not self.imported:
self.bot.gotourl('csv2db/import_db_1')
self.imported = True
self.bot.goto('login')
self.bot.JS__fillform('login')
self.bot.goto('receiver_profile')
self.bot.JS__fillform('receiver_profile')
try:
self.bot._driver.execute_script("var e = $('.icon-zoom-in'); e[e.length-1].click()")
except:
print "unable to access selector id = view"
pass
try:
self.bot._driver.execute_script("var e = $('.icon-pencil'); e[e.length-1].click()")
except:
print "unable to access selector id = edit"
pass
self.bot.JS__fillform('receiver_profile')
self.bot.goto('logout')
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert.text
finally: self.accept_next_alert = True
@classmethod
def setUpClass(cls):
environ['NO_PROXY'] = '127.0.0.1' # IP-address of Jenkins server
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == '__main__':
print('Starting at: "%s"' % time.asctime())
unittest.main()
print('Finished at: "%s"' % time.asctime())
i solved the problem in this way
Get a portable version of Firefox from here (get a version which was working with you for example i got Firefox version 23) and extract it to specific directory
import os
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(os.path.join('Pathto','FirefoxPortable','FirefoxPortable.exe'))
driver = webdriver.Firefox(firefox_binary=binary)
this has been tested with selenium 2.37.2
and 2.39.0
Enjoy!
From what I can tell, this error is caused by the browser being shut down/killed/crashing and selenium being unable to handle the response that comes back (empty response). I included the details here:
How to deal with sporadic BadStatusLine, CannotSendRequest errors in python WebDriver
But it's a pretty common situation where firefox versions and selenium versions are incompatible. I would just pick the most stable versions and stick to those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With