Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active May 27, 2017
Embed
What would you like to do?
【Python】Seleniumでページを表示させる前後にイベントを登録する方法
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
import time
import datetime
try:
class MyListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
"""
ページが表示される前の処理。
10秒待ってからページを表示させます。
"""
print("Before navigate to %s" % url)
print(datetime.datetime.now())
time.sleep(10)
def after_navigate_to(self, url, driver):
"""
ページが表示された後の処理。
"""
print("After navigate to %s" % url)
print(datetime.datetime.now())
driver = Firefox()
ef_driver = EventFiringWebDriver(driver, MyListener())
ef_driver.get("https://a-zumi.net")
except Exception as e:
print(e)
finally:
ef_driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment