【问题标题】:Changing user-agent in chrome Not Working in selenium webdriver python在 chrome 中更改用户代理在 selenium webdriver python 中不起作用
【发布时间】:2021-09-28 16:26:40
【问题描述】:
    self.options.add_argument("--headless")
    self.options.add_argument('--no-sandbox')
    self.options.add_argument("disable-infobars")
    # self.options.add_argument('--start-maximized')
    # self.options.add_argument('--start-fullscreen')
    self.options.add_argument('--single-process')
    self.options.add_argument('--disable-dev-shm-usage')
    self.options.add_argument(
        '--disable-blink-features=AutomationControlled')
    self.options.add_experimental_option('useAutomationExtension', False)
    self.options.add_experimental_option("excludeSwitches",
                                         ["enable-automation"])
    self.options.add_argument("log-level=3")
    # self.options.add_argument("--incognito")
    
    self.options.add_argument(
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'
    )
    

    # Change chrome driver path accordingly
    self.driver = webdriver.Chrome(
        executable_path="B:\mydriver\Resources\chromedriver.exe",
        chrome_options=self.options,
    )
    self.driver.set_window_size(1400, 920)
    self.waitdriver = WebDriverWait(self.driver, 10)
    self.locators = Locators()
    self.driver.get('https://www.facebook.com')
    self.driver.save_screenshot(ROOT_DIR + r"\Temp\Debug\Error1.PNG")
    a = self.driver.execute_script("return navigator.userAgent")
    print(a)
>>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/96.0.4643.0 Safari/537.36

它与我提供给它的不同。我尝试更改其他用户代理,但似乎同样的问题。我正在使用铬的二进制文件。

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python selenium selenium-chromedriver undetected-chromedriver


【解决方案1】:
self.options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36')

通过在用户代理之前添加用户代理解决。 You can check here for much more browser agent.To just rotate then using random.

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多