Skip to content Skip to sidebar Skip to footer

Python/Selenium/PhantomJS - Data Retained Between Execution

I am trying to learn Selenium. I am using Python 2.7. Phantom JS - 2.1.1. Background - The script is trying to enter data into the controls. The script is able to catch hold of con

Solution 1:

The site is probably caching user input in cookies or in local storage. Sites will typically do this to allow you to navigate back and forth between pages, or to return to a form later without having to fill in all the details again. For example, this is how a site might persist state in React.

You can use your browser's dev tools to find out. For example, here's how you'd do that in Chrome. The image shows the various types of storage that might be in use.

Chrome dev tools local storage inspection

If you want to start each test without any previous input you'll need to delete it. If the site stores cookies and you don't have any other cookies you want to save, you can delete all of them:

driver.delete_all_cookies()

It's also possible to delete individual cookies.

If the site uses local storage it's a little tricker with python, for the moment, because it doesn't look like the python bindings implement a means to access local storage, like java does. I could be wrong. But you can use javascript, like so:

driver.execute_script('window.localStorage.clear();')

That will delete all of the local storage associated with the current domain. As with cookies there are ways to access individual items, if necessary.


Post a Comment for "Python/Selenium/PhantomJS - Data Retained Between Execution"