'Save localStorage for next run in Selenium Python Firefox

I'm using Selenium in Python with Firefox browser.

I want to save cookies and localStorage between each Selenium sessions. I first tried start Firefox using specified Profile. But it reports me:

DeprecationWarning: Setting a profile has been deprecated. Please use the set_preference and install_addons methods

So I believe there are something replacement that do the same thing for me.

The website I'm working on use cookies and localStorage to keep its session. I searched about how to read and write cookies, and I luckily got some useful codes. But I haven't find out anything useful for localStorage.

Some posts suggest me use execute_script to read / write localStorage. But it looks like that I had to open the website first so I can run execute_script on it. But the website require correct localStorage configured to load correctly. Or in the other word, I need set localStorage before the website get loaded.

So is there any way I can keep localStorage between each Selenium sessions?



Solution 1:[1]

I believe this is what you're trying to do. Most of your code is right except for you attaching the onChange function to the onClick property instead of the onChange property

I've attached a basic example below because you used custom html elements in your code that I don't have access to.

const people = [
  {
    name: "John Doe",
    age: 26,
  },
  {
    name: "Jason Bourne",
    age: 32,
  },
  {
    name: "Steve Wang",
    age: 19,
  },
];

function App() {
  const [option, setOption] = React.useState(null);
  const [text, setText] = React.useState("");

  const onChange = (ev) => {
    const val = ev.target.value;
    setOption(val);

    const person = people[val];
    if (person) {
      setText(person.name + " is " + person.age);
    }
  };

  return (
    <div>
      <select value={option} onChange={onChange}>
        {people.map((person, index) => (
          <option value={index} key={index}>
            {person.name}
          </option>
        ))}
      </select>

      <br />

      <input
        type="text"
        value={text}
        onChange={(ev) => setText(ev.target.value)}
      />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@17/umd/react.production.min.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" crossorigin></script>
<div id="root"></div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1