简体   繁体   English

Python Selenium `execute_cdp_cmd` 仅在第一次运行时有效

[英]Python Selenium `execute_cdp_cmd` only works at the first run

I am trying to change device geolocation using Selenium Python (with Selenium wire to catch http requests) by:我正在尝试使用 Selenium Python(使用 Selenium 线来捕获 http 请求)通过以下方式更改设备地理位置:

from seleniumwire import webdriver

options = webdriver.EdgeOptions()
options.accept_insecure_certs = True
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Edge(seleniumwire_options={
                        'port': 12345, 'disable_encoding': True}, options=options)

# List of coordinates
lats = [44.55, 43.63, 52.25, 45.48, 47.35]
longs = [-77.33, -80.33, -81.62, -76.81, -84.62]

for i in range(len(lats)):
    coordinates = {
        "latitude": lats[i],
        "longitude": longs[i],
        "accuracy": i + 1
    }

    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinates)
    driver.get(some_website)

    # I can see the coordinates passed to the `execute_cdp_cmd` changed every loop
    print(coordinates)

However, the actual geolocation will always be the first coordinates.然而,实际的地理位置总是第一个坐标。 The only way is to create a new driver at each loop but that's really bad performance.唯一的方法是在每个循环中创建一个新的驱动程序,但这真的很糟糕。 What am I doing wrong?我究竟做错了什么?

Have you tried using Emulation.clearGeolocationOverride prior to calling Emulation.setGeolocationOverride ?在调用Emulation.setGeolocationOverride Emulation.clearGeolocationOverride

from seleniumwire import webdriver

options = webdriver.EdgeOptions()
options.accept_insecure_certs = True
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Edge(seleniumwire_options={
                        'port': 12345, 'disable_encoding': True}, options=options)

# List of coordinates
lats = []
longs = []

for i in range(len(lats)):
    coordinates = {
        "latitude": lats[i],
        "longitude": longs[i],
        "accuracy": i + 1
    }

    driver.execute_cdp_cmd("Emulation.clearGeolocationOverride")
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", coordinates)
    driver.get(some_website)

    print(coordinates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM