Selenium 4.x 正确执行 "Page.addScriptToEvaluateOnNewDocument"
Selenium 4.x execute "Page.addScriptToEvaluateOnNewDocument" properly
我遇到了一个奇怪的问题,我真的找不到解决办法。我的生产网站需要一些测试,当然机器人检查已启用(未命名)。
根据我使用“Page.addScriptToEvaluateOnNewDocument”后的测试,结果不是我所期望的。
- 如果我直接在 Chromium 的当前选项卡中访问该网站,它将无法通过机器人检查。
- 如果我手动打开一个新标签并访问该网站,它会通过机器人检查。
起初我以为脚本没有在当前选项卡中执行,但是检查我覆盖的内容显示它们是。
使用我从 puppeteer 获取的这个小脚本在两个选项卡中都是通过:
async function test() {
const results = {}
async function test(name, fn) {
const detectionPassed = await fn()
if (detectionPassed) {
console.log(`WARNING: Chrome headless detected via ${name}`)
} else {
console.log(`PASS: Chrome headless NOT detected via ${name}`)
}
results[name] = detectionPassed
}
await test('userAgent', _ => {
return /HeadlessChrome/.test(window.navigator.userAgent)
})
// Detects the --enable-automation || --headless flags
// Will return true in headful if --enable-automation is provided
await test('navigator.webdriver present', _ => {
return 'webdriver' in navigator
})
await test('window.chrome missing', _ => {
return /Chrome/.test(window.navigator.userAgent) && !window.chrome
})
await test('permissions API', async _ => {
const permissionStatus = await navigator.permissions.query({
name: 'notifications'
})
// eslint-disable-next-line
return (
Notification.permission === 'denied' && // eslint-disable-line no-undef
permissionStatus.state === 'prompt'
)
})
await test('permissions API overriden', _ => {
const permissions = window.navigator.permissions
if (permissions.query.toString() !== 'function query() { [native code] }')
return true
if (
permissions.query.toString.toString() !==
'function toString() { [native code] }'
)
return true
if (
permissions.query.toString.hasOwnProperty('[[Handler]]') && // eslint-disable-line no-prototype-builtins
permissions.query.toString.hasOwnProperty('[[Target]]') && // eslint-disable-line no-prototype-builtins
permissions.query.toString.hasOwnProperty('[[IsRevoked]]') // eslint-disable-line no-prototype-builtins
)
return true
if (permissions.hasOwnProperty('query')) return true // eslint-disable-line no-prototype-builtins
})
await test('navigator.plugins empty', _ => {
return navigator.plugins.length === 0
})
await test('navigator.languages blank', _ => {
return navigator.languages === ''
})
await test('iFrame for fresh window object', _ => {
// evaluateOnNewDocument scripts don't apply within [srcdoc] (or [sandbox]) iframes
// https://github.com/GoogleChrome/puppeteer/issues/1106#issuecomment-359313898
const iframe = document.createElement('iframe')
iframe.srcdoc = 'page intentionally left blank'
document.body.appendChild(iframe)
// Here we would need to rerun all tests with `iframe.contentWindow` as `window`
// Example:
return iframe.contentWindow.navigator.plugins.length === 0
})
// This detects that a devtools protocol agent is attached.
// So it will also pass true in headful Chrome if the devtools window is attached
await test('toString', _ => {
let gotYou = 0
const spooky = /./
spooky.toString = function() {
gotYou++
return 'spooky'
}
console.debug(spooky)
return gotYou > 1
})
return results
};
test();
我的问题是,“Page.addScriptToEvaluateOnNewDocument”真的只能在新标签页中正确执行吗?
我真的不明白为什么这在新标签页而不是当前标签页中起作用。
当前设置和使用:
- Chrome + Chrome 驱动程序:v85.0.4183.83
- Selenium 独立版:4.1.1
参数:
- --no-first-运行 --no-service-auto运行 --no-default-browser-check --disable-blink-features=AutomationControlled --window-size=1280,1024
排除开关:
- 允许预提交输入禁用背景网络
禁用背景遮挡-windows
禁用客户端网络钓鱼检测禁用默认应用程序
禁用挂起监视器禁用弹出窗口阻止禁用提示重新发布
禁用同步启用自动化启用闪烁功能启用日志记录
密码存储测试类型使用模拟钥匙串
首选项:
- profile.default_content_setting_values.popups 1
- profile.default_content_setting_values.cookies 1
- profile.cookie_controls_mode 0
UseAutomationExtension: 错误
请指教,不胜感激
我找到了解决办法。这就是它的工作原理。万岁。
我遇到了一个奇怪的问题,我真的找不到解决办法。我的生产网站需要一些测试,当然机器人检查已启用(未命名)。
根据我使用“Page.addScriptToEvaluateOnNewDocument”后的测试,结果不是我所期望的。
- 如果我直接在 Chromium 的当前选项卡中访问该网站,它将无法通过机器人检查。
- 如果我手动打开一个新标签并访问该网站,它会通过机器人检查。
起初我以为脚本没有在当前选项卡中执行,但是检查我覆盖的内容显示它们是。
使用我从 puppeteer 获取的这个小脚本在两个选项卡中都是通过:
async function test() {
const results = {}
async function test(name, fn) {
const detectionPassed = await fn()
if (detectionPassed) {
console.log(`WARNING: Chrome headless detected via ${name}`)
} else {
console.log(`PASS: Chrome headless NOT detected via ${name}`)
}
results[name] = detectionPassed
}
await test('userAgent', _ => {
return /HeadlessChrome/.test(window.navigator.userAgent)
})
// Detects the --enable-automation || --headless flags
// Will return true in headful if --enable-automation is provided
await test('navigator.webdriver present', _ => {
return 'webdriver' in navigator
})
await test('window.chrome missing', _ => {
return /Chrome/.test(window.navigator.userAgent) && !window.chrome
})
await test('permissions API', async _ => {
const permissionStatus = await navigator.permissions.query({
name: 'notifications'
})
// eslint-disable-next-line
return (
Notification.permission === 'denied' && // eslint-disable-line no-undef
permissionStatus.state === 'prompt'
)
})
await test('permissions API overriden', _ => {
const permissions = window.navigator.permissions
if (permissions.query.toString() !== 'function query() { [native code] }')
return true
if (
permissions.query.toString.toString() !==
'function toString() { [native code] }'
)
return true
if (
permissions.query.toString.hasOwnProperty('[[Handler]]') && // eslint-disable-line no-prototype-builtins
permissions.query.toString.hasOwnProperty('[[Target]]') && // eslint-disable-line no-prototype-builtins
permissions.query.toString.hasOwnProperty('[[IsRevoked]]') // eslint-disable-line no-prototype-builtins
)
return true
if (permissions.hasOwnProperty('query')) return true // eslint-disable-line no-prototype-builtins
})
await test('navigator.plugins empty', _ => {
return navigator.plugins.length === 0
})
await test('navigator.languages blank', _ => {
return navigator.languages === ''
})
await test('iFrame for fresh window object', _ => {
// evaluateOnNewDocument scripts don't apply within [srcdoc] (or [sandbox]) iframes
// https://github.com/GoogleChrome/puppeteer/issues/1106#issuecomment-359313898
const iframe = document.createElement('iframe')
iframe.srcdoc = 'page intentionally left blank'
document.body.appendChild(iframe)
// Here we would need to rerun all tests with `iframe.contentWindow` as `window`
// Example:
return iframe.contentWindow.navigator.plugins.length === 0
})
// This detects that a devtools protocol agent is attached.
// So it will also pass true in headful Chrome if the devtools window is attached
await test('toString', _ => {
let gotYou = 0
const spooky = /./
spooky.toString = function() {
gotYou++
return 'spooky'
}
console.debug(spooky)
return gotYou > 1
})
return results
};
test();
我的问题是,“Page.addScriptToEvaluateOnNewDocument”真的只能在新标签页中正确执行吗?
我真的不明白为什么这在新标签页而不是当前标签页中起作用。
当前设置和使用:
- Chrome + Chrome 驱动程序:v85.0.4183.83
- Selenium 独立版:4.1.1
参数:
- --no-first-运行 --no-service-auto运行 --no-default-browser-check --disable-blink-features=AutomationControlled --window-size=1280,1024
排除开关:
- 允许预提交输入禁用背景网络 禁用背景遮挡-windows 禁用客户端网络钓鱼检测禁用默认应用程序 禁用挂起监视器禁用弹出窗口阻止禁用提示重新发布 禁用同步启用自动化启用闪烁功能启用日志记录 密码存储测试类型使用模拟钥匙串
首选项:
- profile.default_content_setting_values.popups 1
- profile.default_content_setting_values.cookies 1
- profile.cookie_controls_mode 0
UseAutomationExtension: 错误
请指教,不胜感激
我找到了解决办法。这就是它的工作原理。万岁。