使用未检测到的 chromedriver 时,我无法更改用户代理

问题描述 投票:0回答:1

以前有人遇到过这个问题吗?这是我的代码

from selenium import webdriver
import time
import undetected_chromedriver as uc


# Define a custom user agent string
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

# Set up Chrome WebDriver with the custom user agent
chrome_options = uc.ChromeOptions()
chrome_options.headless = False

chrome_options.add_argument(f"user-agent={user_agent}")

# Initialize the WebDriver with the specified options
driver = uc.Chrome(options=chrome_options)

# You can now use the driver with the custom user agent
driver.get("https://amiunique.org/fingerprint")
time.sleep(40)

我已经在不使用 unDetected-chromedriver uc 的情况下测试了此方法,并且 useragent 已更改,但由于某种原因,当我使用 unDetected-chromedriver 时,useragent 拒绝更改,有什么想法吗?

python selenium-webdriver user-agent fingerprint undetected-chromedriver
1个回答
0
投票

它对我有用

get_random_user_agent = random.choice(open('user_agents.txt', 'r').read().splitlines())
    print(get_random_user_agent)
    options.add_argument('--user-agent='+get_random_user_agent)
© www.soinside.com 2019 - 2024. All rights reserved.