Skip to content

Instantly share code, notes, and snippets.

@luzihang123
Last active July 23, 2020 01:44
Show Gist options
  • Save luzihang123/d74a1a9c83e7e3f015401fa9a8e3c9cb to your computer and use it in GitHub Desktop.
Save luzihang123/d74a1a9c83e7e3f015401fa9a8e3c9cb to your computer and use it in GitHub Desktop.
在selenium gird远端中去除webdriver特征
# https://blog.csdn.net/u014291399/article/details/106636619
from selenium.webdriver.chrome.remote_connection import ChromeRemoteConnection
from selenium import webdriver
import time
chromeOptions = webdriver.ChromeOptions()
# chromeOptions.add_argument('-headless') # 设为无头模式
# chromeOptions.add_argument('--user-agent=Mozilla/5.0 HAHA') # 配置对象添加替换User-Agent的命令
chromeOptions.add_argument('--disable-infobars') # 去掉提示:Chrome正收到自动测试软件的控制
chromeOptions.add_experimental_option('excludeSwitches', ['enable-automation'])
chromeOptions.add_experimental_option('useAutomationExtension', False)
chromeOptions.add_argument('--start-maximized') # 最大化运行(全屏窗口),不设置,取元素会报错
with webdriver.Remote(command_executor=ChromeRemoteConnection(
remote_server_addr='http://192.168.95.56:4444/wd/hub',
keep_alive=True),
desired_capabilities={
'platform': 'WINDOWS',
'browserName': "chrome",
'version': '',
'javascriptEnabled': True
},
options=chromeOptions
) as driver:
print(driver.execute("executeCdpCommand", {'cmd': "Page.addScriptToEvaluateOnNewDocument", 'params': {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
}}))
driver.get('http://jr.chengdu.gov.cn/jinrongban/c139013/list.shtml')
time.sleep(5)
print(driver.page_source)
time.sleep(999)
```
pyppeteer 去除webdriver属性
```
import asyncio
from pyppeteer import launch
async def main():
browser = await launch(headless=False)
page = await browser.newPage()
await page.evaluateOnNewDocument('''() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
}
''')
await page.goto('http://www.nmpa.gov.cn/WS04/CL2042/')
await asyncio.sleep(900)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
```
selenium本地去除webdriver属性
```
browser = webdriver.Chrome()
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment