現在我正在使用 python selenium,我想知道一個問題。我在打開的網路瀏覽器中運行 js 腳本
result = driver.execute_script('''
return await fetch("https://example.com", {
"headers": {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",
"traceparent": "00-3e3ff822cf02c630cff68ed054769214-62c0b9ae093c2f05-01",
"Referrer-Policy": "origin"
},
"body": null,
"method": "GET"
})
.then(json => json.json());
''')
我的問題,當我運行 js 腳本并呼叫 api 時,結果回傳“會話已過期”。
為了解決這個問題,我會在 fetch 標頭中添加 cookie 嗎?我想,如果 js 在打開的網路瀏覽器中運行,fetch 會自動處理 cookie。
我錯了嗎?
如果你知道這件事,請回答我。
提前致謝。
uj5u.com熱心網友回復:
您可以設定credentials: "include"cookie 和其他與憑據相關的資訊,例如 HTTP 身份驗證條目和 TLS 客戶端證書與請求一起發送。你可以在這里閱讀更多關于它的資訊。
像這樣的東西應該作業:
return await fetch("https://example.com", {
"headers": {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",
"traceparent": "00-3e3ff822cf02c630cff68ed054769214-62c0b9ae093c2f05-01",
"Referrer-Policy": "origin"
},
"body": null,
"method": "GET",
"credentials": "include" // <---
})
.then(json => json.json());
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/531046.html
上一篇:預期條件失敗:等待元素可點擊:By.xpath://*[@id="documentation"]/div/div[2]/div/button
