selenium Chrome Devtools Protocol calls for analog positioning

Recently doing on number automated support this one, a lot of research in a foreign social media, found that the account attributes associated with the IP address and location, so he went to the line and intends to use selenium scripts for a number of support while achieving in the process of raising numbers use a proxy IP and analog positioning.

Stepped pit

  • chromedriver modify profile.default_content_setting_values ​​to open the browser simulator, but in fact this is not in the profile configuration option
  • chromedriver initialization time, call the manual geolocation plugin that is loaded in the sensor positioned in the browser starts, but can not be activated overwrite

Solution

By CDP, may operate directly on ChromeDriver developer tools (i.e. F12), in webserver.Chrome (). Execute_cdp_cmd execute commands in the CDP method, the latitude and longitude in a dict which passed to the method to complete simulator

CHROME_PATH = ""  # your chromedriver's path
chrome_opt = webdriver.ChromeOptions()
# chrome_opt.add_argument("proxy-server=http://127.0.0.1:1087")  # 加载代理IP
prefs = {
    'profile.default_content_setting_values':
        {
            'notifications': 1,  # 允许通知
            'geolocation': 1  # 允许定位
        },
}
chrome_opt.add_experimental_option('prefs', prefs)
self.driver = webdriver.Chrome(executable_path=CHROME_PATH, desired_capabilities=chrome_opt.to_capabilities())
self.driver.execute_cdp_cmd("Emulation.setGeolocationOverride", cmd_args={'latitude': 37.871093, 'longitude': -122.281604,  'accuracy': 100})

Improve

Read the official document cases using a Remote link mode, its benefits is every time you start chromedriver, are to be connected to a common chromedriver (open port 9515), this way can save a lot of server resources through remote way; and direct connecting webserver.Chrome manner chromedriver, each start time of startup will instantiate a corresponding server, so that the card is not strange sub ~

Ever since the connection chromedriver decisive way change Remote, and start chromedriver Service
$ ./chromedriver

13806270-827e8b4440fd8929.png
Start chromedriver

So the question is, Remote connections like Chrome does not provide the connection execute_cdp_cmd()method. If they do not provide, then go to the source to see how fat IV line and then regenerate a piece of code ...

In the execute_cdp_cmd()source code is found, in fact, this method is directly returnedself.execute()['value']

13806270-3937f0188c3d47f4.png
execute_cdp_cmd

self.execute()['value']The method is in remote / webserver in class WebDriver(object)a statement, Chrome also incorporates the connection type (it seems to be some idea)

Chrome connection back to see the source, found init manner rewritten class WebDriver(object)insideRemoteConnection

13806270-8e3018336fe79e78.png
Chrome.__init__

13806270-7a346fdd2be55aed.png
ChromeRemoteConnection

The original Chrome in connection instantiation statement cdp entrance, so only before theexecute_cdp_cmd()

self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/goog/cdp/execute')

So we'll declare it again cdp in Remote connection instantiation entrance to get away

class NewRemoteConnection(RemoteConnection):
    """
    重写RemoteConnection,加入CDP支持
    """
    def __init__(self, remote_server_addr, keep_alive=True):
        RemoteConnection.__init__(self, remote_server_addr, keep_alive)
        self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/goog/cdp/execute')


class FacebookAutoAccount(object):
    """
    Chrome初始化
    """
    def __init__(self):
        chrome_opt = webdriver.ChromeOptions()
        # chrome_opt.add_argument("proxy-server=http://127.0.0.1:1087")  # 加载代理IP
        prefs = {
            'profile.default_content_setting_values':
                {
                    'notifications': 1,  # 允许通知
                    'geolocation': 1  # 允许定位
                },
        }
        chrome_opt.add_experimental_option('prefs', prefs)
        remote_url = 'localhost:9515'  # 远端调试地址
        self.driver = webdriver.Remote(remote_url, desired_capabilities=chrome_opt.to_capabilities())
        self.driver.command_executor = NewRemoteConnection(remote_url, keep_alive=False)  # 加入executeCdpCommand的支持

Reference Documents

CDP official documents
Chrome remote debugging protocol in automated test application and practice
chrome devtools protocol - Web performance automation practices introduced
ChromeOptions

Guess you like

Origin blog.csdn.net/weixin_34364135/article/details/90822487