使用 Selenium Webdriver 添加 cookie
Adding cookies with Selenium Webdriver
我有以下 Selenium 代码序列,用 Node.js 编写:
it('tests cookies', t => {
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().addCookie({name:'foo', value: 'bar'});
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().deleteCookie('foo');
return driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
});
我得到了这个输出:
all cookies => []
all cookies => []
all cookies => []
有人知道为什么 addCookie 功能不起作用吗?我不确定我是否理解为什么这不会在饼干罐中产生一些饼干。
问题是未定义 cookie 域。在使用 cookie 之前,您需要导航至某些 URL。尝试添加 driver.get('<some_url>')
,然后再获取所有 cookie ,然后 设置新的 cookie。
it('tests cookies', t => {
driver.get('127.0.0.1'); // <-- This will set the domain
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().addCookie({name:'foo', value: 'bar'});
driver.get('127.0.0.1'); // <-- Navigate again after setting a new cookie
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().deleteCookie('foo');
return driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
});
另请参阅:
我有以下 Selenium 代码序列,用 Node.js 编写:
it('tests cookies', t => {
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().addCookie({name:'foo', value: 'bar'});
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().deleteCookie('foo');
return driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
});
我得到了这个输出:
all cookies => []
all cookies => []
all cookies => []
有人知道为什么 addCookie 功能不起作用吗?我不确定我是否理解为什么这不会在饼干罐中产生一些饼干。
问题是未定义 cookie 域。在使用 cookie 之前,您需要导航至某些 URL。尝试添加 driver.get('<some_url>')
,然后再获取所有 cookie ,然后 设置新的 cookie。
it('tests cookies', t => {
driver.get('127.0.0.1'); // <-- This will set the domain
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().addCookie({name:'foo', value: 'bar'});
driver.get('127.0.0.1'); // <-- Navigate again after setting a new cookie
driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
driver.manage().deleteCookie('foo');
return driver.manage().getCookies().then(function (cookies) {
console.log('all cookies => ', cookies);
});
});
另请参阅: