【问题标题】:Change user agent using selenium wire in Chrome在 Chrome 中使用硒线更改用户代理
【发布时间】:2021-07-23 12:13:20
【问题描述】:

我正在尝试更改我的用户代理并将更改后的用户代理打印到终端以检查它是否已成功更改但是我没有运气。

我正在使用 selenium 线并尝试对其进行更改,以便我可以登录到网站的移动版本。由于安全原因,我无法放入我想要的用户代理,但是我已经使用了几天并且没有运气。

请看下面我的代码

driver = webdriver.Chrome('/Users/callum/Desktop/chromedriver')

def拦截器(请求):

del request.headers['User-Agent']

request.headers['User-Agent'] = '####'  

driver.get("https://www.google.com")

我也无法从硒线打印用户代理,我只能使用这种方法。

agent = driver.execute_script("return navigator.userAgent")

打印(代理)

有人可以帮忙吗,不胜感激:)

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver user-agent


    【解决方案1】:
    from seleniumwire import webdriver  # Import from seleniumwire
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
    browser = webdriver.Chrome(chrome_options=chrome_options)
    user_agent = browser.execute_script("return navigator.userAgent;")
    print(str(user_agent))
    # Go to the Google home page
    browser.get('https://www.google.com')
    

    this question 中提到的相同 Chrome 选项也可以在这里使用。有关用户代理字符串的打印,请参阅this question

    【讨论】:

      【解决方案2】:

      查看 Chrome 驱动程序的移动仿真功能:

      https://chromedriver.chromium.org/mobile-emulation

      【讨论】:

        猜你喜欢
        • 2019-06-25
        • 2023-04-08
        • 2018-09-08
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        相关资源
        最近更新 更多