Created
June 27, 2013 20:44
-
-
Save dlai0001/5880241 to your computer and use it in GitHub Desktop.
Keep pinging current URL in another browser thread to keep a browser alive when you do something more expensive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BrowserStandBy(object): | |
""" | |
This class allows you to put a browser on stand by sending no-op commands to keep | |
a selenium session from timing out. | |
""" | |
def __init__(self, webdriver, max_time=WTF_TIMEOUT_MANAGER.EPIC, sleep=5): | |
""" | |
@param webdriver:Webdriver instance to keep alive. | |
""" | |
self.webdriver = webdriver | |
self._sleep_time = sleep | |
self._max_time = max_time | |
def start(self): | |
"Start standing by" | |
self._end_time = datetime.now() + timedelta(seconds = self._max_time) | |
self._thread = Thread(target=lambda: self.__stand_by_loop()) | |
self._keep_running = True | |
self._thread.start() | |
pass | |
def stop(self): | |
self._keep_running = False | |
pass | |
def __stand_by_loop(self): | |
print self._keep_running | |
while datetime.now() < self._end_time and self._keep_running: | |
self.webdriver.current_url #Just performing current_url to keep this alive. | |
time.sleep(self._sleep_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment