我有一个机器人与使用Splinter和Selenium的网站交互。该网站使用Javascript并实时更新。机器人在90%的时间内工作良好,但由于随机事件,它有时会引发异常。对我来说,调试这些事件是非常困难的,当我在调试器中,网站已经改变了。
是否有任何方式我可以记录网站数据并播放它,像vcrpy?或者无论如何我都可以记录行为,以便我可以调试和测试?
你能做的最接近的事情就是在各种事件中截取网页的屏幕截图。你必须使用EventFiringWebDriver。无论您想截取哪个屏幕截图,都可以调用screen_shot
函数。
from selenium.webdriver.support.events import EventFiringWebDriver
from selenium.webdriver.support.events import AbstractEventListener
import os
import time
class ScreenShotListener(AbstractEventListener):
DIR_NAME = None
def screen_shot(self, driver):
dir = os.path.curdir
unique_filename = str(int(time.time() * 1000)) + ".png"
fpath = os.path.join(dir, unique_filename)
driver.get_screenshot_as_file(fpath)
def before_navigate_to(self, url, driver):
pass
def after_navigate_to(self, url, driver):
pass
def before_navigate_back(self, driver):
pass
def after_navigate_back(self, driver):
pass
def before_navigate_forward(self, driver):
pass
def after_navigate_forward(self, driver):
pass
def before_find(self, by, value, driver):
pass
def after_find(self, by, value, driver):
pass
def before_click(self, element, driver):
pass
def after_click(self, element, driver):
pass
def before_change_value_of(self, element, driver):
pass
def after_change_value_of(self, element, driver):
pass
def before_execute_script(self, script, driver):
pass
def after_execute_script(self, script, driver):
pass
def before_close(self, driver):
pass
def after_close(self, driver):
pass
def before_quit(self, driver):
pass
def after_quit(self, driver):
pass
def on_exception(self, exception, driver):
pass
driver = EventFiringWebDriver(driver, ScreenShotListener())