Python Selenium 2.39和Firefox 26

时间:2013-12-08 00:05:25

标签: python firefox selenium-webdriver web2py python-unittest

我试图用unittest脚本执行一些selenium但是我得到以下错误

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)

tearDown()函数中第70行template.py中的错误,这是使用Selenium-IDE自动生成的默认unittest函数

def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)

编辑:

这个问题发生在我将Firefox更新到26时,即使我更新到selenium 2.39.0问题也没有消失


来源:

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

5 个答案:

答案 0 :(得分:1)

我用这种方式解决了这个问题

here获取可移植版本的Firefox(获取与您合作的版本,例如我获得Firefox版本23)并将其解压缩到特定目录

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)

已经使用硒2.37.22.39.0

进行了测试

享受!

答案 1 :(得分:1)

据我所知,此错误是由浏览器关闭/终止/崩溃和selenium无法处理返回的响应(空响应)引起的。我在这里列出了详细信息:

How to deal with sporadic BadStatusLine, CannotSendRequest errors in python WebDriver

但这是火狐版本和selenium版本不兼容的常见情况。我会选择最稳定的版本并坚持下去。

答案 2 :(得分:0)

不确定您尝试过的selenium 2.38的哪个子版本,但此问题似乎已在 2.38.4 中修复。这是issue link

答案 3 :(得分:0)

您可以尝试以下代码:

from os import environ

在你的测试用例类中:

@classmethod
def setUpClass(cls):
    environ['NO_PROXY'] = '127.0.0.1'  # IP-address of Jenkins server

可能我遇到了类似的问题: Django Jenkins raises WebDriverException when processed to Selenium server

答案 4 :(得分:0)

在Python 2.7.0(Windows 8操作系统)上使用Firefox 26运行Selenium 2.39.0时,我也收到了CannotSendRequest()错误。我通过降级到Selenium 2.35.0解决了这个问题:

pip uninstall selenium
pip install selenium==2.37.0

运行这些命令2.35.0。验证:

import selenium
print selenium.__version__

然后我downloaded Firefox 25.0.1告诉Selenium使用以下代码行加载该版本:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)

检查正在加载的Firefox版本:

from selenium import webdriver
driver = webdriver.Firefox()
print driver.capabilities['version']

到目前为止,错误并未重新出现。