我正在使用非常复杂的设置来测试各种非公开网页。我用来在图像中jenkins运行python- selenium测试docker。这样,我完全可以独立于詹金斯环境,并且可以创建自己的环境。在这种环境中,我安装了以下软件:
jenkins
python- selenium
docker
selenium测试创建WebDriver以下方法:
WebDriver
profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.dir", self.downloadpath) profile.set_preference("browser.download.folderList", 2) profile.set_preference("browser.download.useDownloadDir", True) profile.set_preference("browser.download.manager.showWhenStarting", False) profile.set_preference("pdfjs.disabled", True) profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/force-download, image/png, text/html, text/plain, " "image/tiff, text/csv, application/zip, application/octet-stream") profile.set_preference("browser.download.manager.alertOnEXEOpen", False) profile.set_preference("browser.download.manager.focusWhenStarting", False) profile.set_preference("browser.helperApps.alwaysAsk.force", False) profile.set_preference("browser.download.manager.alertOnEXEOpen", False) profile.set_preference("browser.download.manager.closeWhenDone", True) profile.set_preference("browser.download.manager.showAlertOnComplete", False) profile.set_preference("browser.download.manager.useWindow", False) profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False) self.driver = webdriver.Firefox(profile, log_path = logfile)
其中logfile和self.downloadpath是docker设置中的两个有效路径。
logfile
self.downloadpath
整个测试套件包括6个独立的测试用例,每个用例都具有与上述相同的设置。它们通常运行良好且完整无问题。
但是, 如果不对 测试或常规设置 进行任何更改 ,则测试有时会失败,并显示以下错误消息:
File "/root/tests/bsp_usecase_tests/tools/basicsuite.py", line 210, in set_driver_firefox self.driver = webdriver.Firefox(profile, log_path = logfile) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 158, in __init__ keep_alive=True) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__ self.start_session(desired_capabilities, browser_profile) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response raise exception_class(message, screen, stacktrace) WebDriverException: Message: connection refused
我有两个问题:
附录 :
time.sleep(10)
webdriver.Firefox
您看到的错误是:
WebDriverException: Message: connection refused
根据文档 WebDriverException , 基本webdriver异常 如下:
WebDriverException
exception selenium.common.exceptions.WebDriverException(msg=None, screen=None, stacktrace=None)
因此connection is refused here意味着 Selenium 无法通过以下方式建立您想要建立的连接:
connection is refused here
Selenium
self.driver = webdriver.Firefox(profile, log_path = logfile)
一种可能的解决办法是提供的完整名称logfile与沿logical location的logfile(从Project Level)如下:
logical location
Project Level
self.driver = webdriver.Firefox(firefox_profile=profile, log_path='./Log/geckodriver.log')
再次,正如您提到的When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore,因此我假设以前有一个实例 GeckoDriver 和一个 Firefox Browser 客户端处于活动状态。因此,与@Florent B.所提到的类似,您必须屏蔽脚本以防止遇到“ 竞跑条件” ,而后者可能源于以下任一情况:
When I used a time.sleep(10) just before the webdriver.Firefox line, the error did not show up anymore
GeckoDriver
Firefox Browser
Marionette
Docker Image
根据上述原因,您需要执行以下几个步骤:
Selenium-Python
WebDriver variant
Web Browser
tearDown()
webdriver
webclient
Clean
IDE
Test Suite
Clear
Tests
CCleaner
rust_mozprofile directories