Python with selenium: rerun on pre-existing browser
Question: I'm using Python with Selenium 2.44. When the test fails, I can't just
uncomment all the code before the failure when debugging it, because the
driver will not be declared for the browser. Therefore, whenever I try fixing
something, I always have to open a new browser in the test case. This is
rather... slow since I have to login, which adds an additional 30 seconds (not
devastating, but annoying). I want to know if there's a way for me to just
continue a session, or do something that allows me to start the test midway
through (so if I have the webpage open already, I can just immediately start
clicking things rather than opening a new browser). Is this possible?

For example, if I had something along the lines of:

    
    
    driver = webdriver.Firefox()
    driver.get("google.com")
    driver.find_element_by_xpath("//input[@id='gbqfq']").send_keys("cats" + Keys.RETURN)
    

This should open Firefox, go to google, and search for cats. Pretend like
there's a ton of stuff you have to do before you can actually make it to the
google page, though. Now if it were to fail on the search for cats, the only
way I would be able to test to see if I fixed the code would be to rerun the
test (`webdriver.Firefox()` would open a new browser). Rather than that,
assuming I'd still have google open, I'd like the selenium test to just start
off on the previous browser and google page (therefore saying the first step
in the code would be the `send_keys("cats")`). Is this possible?

I think that this was a similar question, but it didn't get checked off as
answered: [How to resume browser session or use existing browser window with
Selenium-Python?](http://stackoverflow.com/questions/11233225/how-to-resume-
browser-session-or-use-existing-browser-window-with-selenium-pytho)

This one also seems similar, only pertaining to Java: [How do I rerun Selenium
2.0 (webdriver) tests on the same
browser?](http://stackoverflow.com/questions/5383958/how-do-i-rerun-
selenium-2-0-webdriver-tests-on-the-same-browser)

Thanks.


Answer: Look into pdb: <https://docs.python.org/2/library/pdb.html>

Placing this in your code will stop the progression of the test as is until
you tell it to continue in your shell.

Using your code snippit:

    
    
    from pdb import set_trace
    driver = webdriver.Firefox()
    driver.get("google.com")
    set_trace()
    driver.find_element_by_xpath("//input[@id='gbqfq']").send_keys("cats" + Keys.RETURN)
    

will stop your execution after getting the url, allow you to tinker, and then
continue from where the test left off.

Alternatively, while debugging, you can just remove the driver.quit()
statement, wherever it happens to be, which will keep the browser open
wherever your assertion failed. But if you're using a framework like Django
with the LiveTestServer Client, you won't have access to browse the site
further. pdb will allow you to keep the test server active.

