Skip to content

Commit 2ceb1be

Browse files
authored
fix: switch to persistent local Chrome with specific UA to bypass WAF
- Switched Playwright channel from 'msedge' to 'chrome'. - Implemented `launchPersistentContext` to save cookies and "trusted device" states across runs, bypassing the shadow ban (fake password error). - Updated User-Agent to match Chrome 145. - Fixed page initialization to use the default tab created by persistent context.
1 parent bab6846 commit 2ceb1be

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

main.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,17 @@ async function main() {
9696
console.log("🚀 Starting OAuth automation task...");
9797
const authUrl = `https://api.schwabapi.com/v1/oauth/authorize?client_id=${APP_KEY}&redirect_uri=${REDIRECT_URI}`;
9898

99-
const browser = await chromium.launch({
99+
const userDataDir = './schwab-chrome-session';
100+
101+
const context = await chromium.launchPersistentContext(userDataDir, {
100102
headless: false,
101-
channel: 'msedge',
103+
channel: 'chrome', // 💡 改成了调用本地真实的 Google Chrome
102104
args: [
103105
'--disable-blink-features=AutomationControlled',
104-
'--no-sandbox',
105-
'--disable-setuid-sandbox',
106-
'--disable-infobars',
107-
'--window-size=1920,1080',
108-
'--window-position=-32000,-32000'
109-
]
110-
});
111-
112-
const context = await browser.newContext({
113-
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
114-
viewport: { width: 1920, height: 1080 },
115-
locale: 'en-US',
116-
timezoneId: 'America/New_York',
106+
'--disable-infobars'
107+
],
108+
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36',
109+
viewport: null,
117110
deviceScaleFactor: 1,
118111
hasTouch: false,
119112
isMobile: false,
@@ -123,11 +116,9 @@ async function main() {
123116
await context.addInitScript(() => {
124117
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
125118
window.navigator.chrome = { runtime: {} };
126-
Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3] });
127-
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
128119
});
129120

130-
const page = await context.newPage();
121+
const page = context.pages().length > 0 ? context.pages()[0] : await context.newPage();
131122

132123
let interceptedCode = null;
133124
page.on('request', request => {

0 commit comments

Comments
 (0)