![](/img/trans.png)
[英]Python Selenium Chromedriver - Can't Get current_url of new opened tab after click()
[英]Python selenium webdriver - driver suddenly “dies” and can't quit, get current_url, open pages
有時,在我的腳本中間,我的webdriver實例將會死 !
從那以后,我無法調用它的任何方法。
一些例子:
>>> spsel.driver.current_url
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 414, in current_url
return self.execute(Command.GET_CURRENT_URL)['value']
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute
return self._request(url, method=command_info[0], data=data)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request
response = opener.open(request)
File "/usr/lib/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
>>> spsel.driver.quit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 55, in quit
RemoteWebDriver.quit(self)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 443, in quit
self.execute(Command.QUIT)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute
return self._request(url, method=command_info[0], data=data)
File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request
response = opener.open(request)
File "/usr/lib/python2.6/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib/python2.6/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
任何想法為什么會出現這種情況? 任何克服的最佳實踐解決方案?
我想在try塊中偶爾通過driver.current_url
測試活性,如果它拋出異常,然后將驅動程序設置為None
,然后重新實例化它......但這是一個丑陋的黑客,我不知道理解為什么需要它。
經過無休止的努力與硒驅動程序和FF擴展分手。 我完全刪除了它。
我使用http://www.phantomjs.org/這是無頭的JS lib。 像魅力一樣工作。 (我想要看到頁面,你可以隨時進行屏幕拍攝)
我主要在ruby工作:所以用poltergeist替換capybara-webkit(這只是一個jy_driver for capybara)
我很確定會有類似的解決方案。 也許這不能回答你的問題,但它會提供關於js測試的不同看法。
我遇到了同樣的問題,或者至少我是這么認為的。 在我這邊瀏覽器(Chrome)將被阻止,當手動停止終端中的進程時,我會得到相同的URLError。
當時我正在使用Django的LiveServerTestCase以及Splinter並實現以下方法:
@classmethod
def setUpClass(cls):
cls.browser = Browser()
super(MyClass, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.browser.quit()
super(MyClass, cls).tearDownClass()
這將為該類運行的所有測試僅創建一個驅動程序。 我還將所有測試都放在一個方法中。
無論如何,這會導致驅動程序在某些時候出現阻塞。
然后我切換到實現Setup和TearDown方法實例化/退出其中的驅動程序。 然后,這將為類中的每個測試(方法)創建並退出驅動程序。 我還將測試分解為幾種方法。
在那之后一切都會運行得很好,而我正在做同樣的事情。 所以問題似乎主要是用一個驅動程序做所有事情。
作為提示,您可以將登錄內容實現到Setup方法中,以便您的驅動程序在每次測試時都會登錄,這是必要的,因為退出驅動程序也會刷新會話。
這就是最終結果:
from splinter import Browser
from django.test import LiveServerTestCase
from django.core.urlresolvers import reverse
class MySeleniumTests(LiveServerTestCase):
fixtures = ['initial_data.json']
def setUp(self):
#fire up your driver
self.browser = Browser('chrome')
#login
self.browser.visit('%s%s' % (self.live_server_url, reverse('home')))
self.assertEquals(self.browser.url, '%s%s' % (self.live_server_url,'/accounts/login/?next=%s' % reverse('home')))
self.browser.fill_form({'username': 'test', 'password': 'test'})
self.browser.find_by_tag('button').first.click()
self.assertEquals(self.browser.url, '%s%s' % reverse('home')))
def tearDown(self):
#quit your driver
self.browser.quit()
您是否先運行selenium webserver?
這個問題表明Web服務器沒有運行就是問題: Python中的Selenium
then the solution is most likely that you need get the selenium server running first. In the download for SeleniumRC you will find a file called selenium-server.jar (as of a few months ago, that file was located at SeleniumRC/selenium-server-1.0.3/selenium- server.jar). On Linux, you could run the selenium server in the background with the command java -jar /path/to/selenium-server.jar 2>/dev/null 1>&2 &
您可以在此處找到有關如何設置服務器的更完整說明http://seleniumhq.org/docs/05_selenium_rc.html#installation
此頁面有類似的問題: http : //johnmudd.infogami.com/blog/5be6
另一個類似的問題: 如何使用Python + Webdriver遠程連接
因此,對於Firefox中的selenium web驅動程序,這是一個“連接被拒絕”錯誤,它實現為Firefox擴展。 這里的猜測是擴展中的一些問題,或者Firefox會阻止擴展中的httpd代碼工作。
您可以嘗試檢查是否具有最新的Web驅動程序擴展和兼容的Firefox版本。
此外,您可能還想嘗試其他瀏覽器,例如Chrome Web Driver(它需要在您的python代碼中進行幾行更改)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.