Webdriver中的LocalStorage

Webdriver中的LocalStorage

本文介绍了Selenium Webdriver中的LocalStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

普通浏览器和Selenium浏览器是否有不同的LocalStorage存储?当我在硒铬上创建项目时,关闭浏览器后,该项目消失了.这是故意的吗?我也无法从普通浏览器中读取localStorage

更具体地说:

如果我在Selenium chrome浏览器中输入控制台

localStorage.setItem("test", "This is a test value");localStorage.getItem("test"); =>按预期打印这是一个测试值"

但是如果我关闭Selenium chrome并重新打开它,然后尝试从同一页面获取相同的值localStorage.getItem("test"); => null

正如我从不同的帖子中读到的那样,它们通常可以与Selenium中的localStorage一起使用.

解决方案

Javascript/Node

由于先前的答案所解释的原因,我遇到了同样的问题;本地存储是按配置文件进行的,Selenium每次打开时都带有一个新的配置文件和一个新的空本地存储.

要在启动硒页面时保持本地存储,请使用相同的配置文件:

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

const chromeProfilePath = 'C:\\Users\\Bob\\AppData\\Local\\Google\\Chrome\\User Data';

let options = new chrome.Options();
options.addArguments(`--user-data-dir=${chromeProfilePath}`);
let driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

您可以通过在浏览器中输入chrome://version来获取配置文件路径.

重要:删除默认"从该路径的末尾开始,因为chrome重新将其重新添加.

此外,要在v74之后对Chrome/ChromeDriver进行更改,还需要对selenium-webdriver进行相关更改,以使选项起作用-确保您具有合适的selenium-webdriver版本.

Are there different LocalStorage stores for normal Browser and Selenium Browser? When i create an item on selenium chrome, after i close the browser the item is gone. Is this intended? Also i can't read the localStorage from the normal Browser

Edit: To be more specific:

If i enter in the console on my Selenium chrome browser

localStorage.setItem("test", "This is a test value");localStorage.getItem("test"); => prints "This is a test value" as intended

But if i close the Selenium chrome and reopen it and try to get the same value from the same page localStorage.getItem("test");=> null

As i have read from different posts, they can normally work with localStorage in Selenium.

解决方案

Javascript / Node

I had the same problem for the reasons explained by previous answers ; local storage is per profile and Selenium opens with a new profile and a new empty local storage each time.

To keep the local storage across selenium page launches, use the same profile:

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

const chromeProfilePath = 'C:\\Users\\Bob\\AppData\\Local\\Google\\Chrome\\User Data';

let options = new chrome.Options();
options.addArguments(`--user-data-dir=${chromeProfilePath}`);
let driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

You can get the profile path by typing chrome://version in the browser.

IMPORTANT: remove the "default" from the end of that path as chrome adds it back on again.

Also, changes to Chrome/ChromeDriver post v74 require associated changes in selenium-webdriver for options to work - make sure you have the appropriate version of selenium-webdriver.

这篇关于Selenium Webdriver中的LocalStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:32