Python 3.x 使用selenium.webdriver实现响应更快的Cherrypy开发环境
我一直在尝试在cherrypy服务器启动时使用selenium模块打开浏览器 我想让它重新加载cherrypy.Autoreload页面,这样我就不必使用鼠标了 作为一个cherrypy插件,如果它启动得太早,服务器将不响应,并在结束会话时抛出一个错误 我需要一个后服务器启动事件或其他什么。Python 3.x 使用selenium.webdriver实现响应更快的Cherrypy开发环境,python-3.x,selenium,webdriver,cherrypy,web-development-server,Python 3.x,Selenium,Webdriver,Cherrypy,Web Development Server,我一直在尝试在cherrypy服务器启动时使用selenium模块打开浏览器 我想让它重新加载cherrypy.Autoreload页面,这样我就不必使用鼠标了 作为一个cherrypy插件,如果它启动得太早,服务器将不响应,并在结束会话时抛出一个错误 我需要一个后服务器启动事件或其他什么。 任何建议???您可以使用cherrypy发动机(总线)的启动阶段 欲了解更多信息:所以我多少想了想。 我使用cherryd创建了一个守护进程服务器: from selenium import webdriv
任何建议???您可以使用cherrypy发动机(总线)的
启动阶段
欲了解更多信息:所以我多少想了想。
我使用cherryd创建了一个守护进程服务器:
from selenium import webdriver
import cherrypy,time,os,signal
class CpIde():
def __init__(self):pass
@cherrypy.expose
def index(self):return "hello there"
@cherrypy.expose
def open(self):
if hasattr(self,'driver'):
try:self.driver.quit()
except:
self.driver.service.process.send_signal(signal.SIGTERM)
self.driver=webdriver.Firefox()
return "Opening FF"
@cherrypy.expose
def start(self):
try:
self.driver.get('http://127.0.0.1:8080')
except:pass
finally:
self.driver.execute_script("""
document.body.innerHTML="Just one second";
window.setTimeout(function(){window.location.reload(false)},300) """
)
return "starting FF"
@cherrypy.expose
def close(self):
self.driver.quit()
del self.driver
return 'Closing FF'
@cherrypy.expose
def refresh(self):
self.driver.execute_script("""
document.body.innerHTML="Just one second";
window.setTimeout(function(){window.location.reload(false)},300) """)
return "restarting"
cherrypy.tree.mount(CpIde(),'/')
然后我制作了一个插件,向它发出请求:
import cherrypy,time,os
from cherrypy.process import wspbus, plugins
import requests as r
def command(method):
addr='http://127.0.0.1:3131/'
r.get(addr+method)
class CpIdePlugin(plugins.SimplePlugin):
def __init__(self,bus):
plugins.SimplePlugin.__init__(self,bus)
self.bus.log('Init plug')
DriveId=os.getenv('CherryDrive')
self.running=False
if DriveId:
self.running=True
else:
command('open')
os.putenv('CherryDrive','True')
def start(self):
self.bus.log('Running FF plugin')
if self.running:
self.refresh()
else:
command('start')
def refresh(self):#need to make a channel i think
self.bus.log("Reload via bus")
command('refresh')
它与我的主应用程序相关联:
import cpideplugin
class root:pass
cherrypy.tree.mount(root(),"/")
cpideplugin.CpIdePlugin(cherrypy.engine).subscribe()
cherrypy.engine.start()#the pubsub engine
cherrypy.server.start()#an html server channel on the engine
cherrypy.engine.block()
cpideplugin.command('close')
这样,selenium驱动程序就不会使用Autoreload刷新,我可以从vim自由工作。这不是一个完美的代码,如果有任何建议,我将不胜感激。干杯效果很好,但每次编辑都会打开一个选项卡。最后,我必须关闭它们或按常规操作。是的,webbrowser
部分是一个演示,我重点介绍了重新启动时的运行方式,您必须实现Selenium代码来重新加载浏览器(使用webdriver创建的)。
import cpideplugin
class root:pass
cherrypy.tree.mount(root(),"/")
cpideplugin.CpIdePlugin(cherrypy.engine).subscribe()
cherrypy.engine.start()#the pubsub engine
cherrypy.server.start()#an html server channel on the engine
cherrypy.engine.block()
cpideplugin.command('close')