Python中的Selenium移除window.navigator.webdriver

移除window.navigator.webdriver需要使用 Google 的Chrome Devtools-Protocol(Chrome 開發工具協議)簡稱 CDP1

Selenium支持使用CDP2。如果是在本地調試,可以參照https://www.cnblogs.com/presleyren/p/12936553.html 這位大神的寫法。

本地初始化方式

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.Chrome(options=chromeOptions) as driver:
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
               Object.defineProperty(navigator, 'webdriver', {
                 get: () => undefined
               })
             """
    })
    driver.get('https://xxxxxxxxxx')

但我這裏是部署了Selenium Grid集羣,連接集羣的客戶端driver是沒有execute_cdp_cmd這個函數的。通過閱讀源代碼發現,所有的調用過程其實是通過Http方式調用,使用的是同一個接口,要使用遠程某個功能,就可以將功能名當作參數發送出去。

邏輯爲:

def execute_cdp_cmd(self, cmd, cmd_args):
    """
    Execute Chrome Devtools Protocol command and get returned result

    The command and command args should follow chrome devtools protocol domains/commands, refer to link
    https://chromedevtools.github.io/devtools-protocol/

    :Args:
     - cmd: A str, command name
     - cmd_args: A dict, command args. empty dict {} if there is no command args

    :Usage:
        driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': requestId})

    :Returns:
        A dict, empty dict {} if there is no result to return.
        For example to getResponseBody:

        {'base64Encoded': False, 'body': 'response body string'}

    """
    return self.execute("executeCdpCommand", {'cmd': cmd, 'params': cmd_args})['value']

所以,如果你使用了遠程Grid集羣,則初始化代碼爲:

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://remotehost:5555/wd/hub',
    keep_alive=True),
        desired_capabilities={
            'platform': 'ANY',
            '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('https://xxxxxxxxxx')

  1. CPD 的官方文檔: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-addScriptToEvaluateOnNewDocument ↩︎

  2. Selenium官方文檔: https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#selenium.webdriver.chrome.webdriver.WebDriver.execute_cdp_cmd ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章