我需要使用 Firefox 和 Python 通过 Webdriver 添加 cookie。它在正常模式下工作,但在private模式下无效。
from selenium import webdriver
capabities = webdriver.DesiredCapabilities.FIREFOX
capabities.update({"javascriptEnabled":True})
firefoxProfile = FirefoxProfile()
firefoxProfile.set_preference("browser.privatebrowsing.autostart", True)
driver = webdriver.Firefox(desired_capabilities=capabities, firefox_profile=firefoxProfile)
driver.get("http://httpbin.org/cookies")
driver.add_cookie({"name":"drag", "value": "lol", "domain": "httpbin.org"})
driver.get("http://httpbin.org/cookies")
Run Code Online (Sandbox Code Playgroud)
无论我刷新驱动程序多少次,cookie 都不会加载。document.cookie控制台日志中没有任何内容。它在 Chrome(未在隐身模式下测试)和 Firefox(非私有)中运行良好。
我知道在 selenium 私有模式中是多余的,我已经阅读了这个 SO 问题以及这个.
但我无法更改大部分代码。即使在 Firefox 的私有模式下,我也需要这个功能来设置 cookie。
火狐 66.0.1
Geckodriver 0.23.0 ( 2018-10-04)
Python 硒 3.14.1
编辑 1
我已经用 chrome(incognito) 测试过,它似乎工作
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
capabities = webdriver.DesiredCapabilities.CHROME
capabities.update({"javascriptEnabled":True})
driver …Run Code Online (Sandbox Code Playgroud) mapfile = {
1879048192: 0,
1879048193: 0,
1879048194: 0,
1879048195: 0,
1879048196: 4,
1879048197: 3,
1879048198: 2,
1879048199: 17,
1879048200: 0,
1879048201: 1,
1879048202: 0,
1879048203: 0,
1879048204: 4,
# intentionally missing byte
1879048206: 2,
1879048207: 1,
1879048208: 0 # single byte cannot make up a dword
}
_buf = {}
for x in (x for x in mapfile.keys() if 0==x%4):
try:
s = "0x{0:02x}{1:02x}{2:02x}{3:02x}".format(mapfile[x+3],mapfile[x+2],mapfile[x+1],mapfile[x+0])
print "offset ", x, " value ", s
_buf[x] = int(s, 16)
except KeyError as …Run Code Online (Sandbox Code Playgroud) 运行 puppeteer 时代理身份验证失败。
傀儡师版本:1.8
平台/操作系统版本:MacOS 10.13.6
Node.js版本:v10.9.0
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless:false,
ignoreHTTPSErrors:true,
devtools:true,
timeout:3000,
args: ['--no-sandbox','--proxy-server=xxx:xxx']
});
const user='xxx';
const password='xxx';
const page = await browser.newPage();
// await page.setExtraHTTPHeaders({
// 'Proxy-Authorization': 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64'),
// });
await page.authenticate({username:user, password:password});
await page.goto('https://www.apple.com/');
let title = await page.title();
console.log("title:" + title);
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
出现错误:
(node:5858) UnhandledPromiseRejectionWarning:
Error: net::ERR_TUNNEL_CONNECTION_FAILED at https://www.apple.com/
at navigate (/xxx/node_modules/_puppeteer@1.8.0@puppeteer/lib/Page.js:622:37)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:5858) UnhandledPromiseRejectionWarning:
Unhandled promise …Run Code Online (Sandbox Code Playgroud)