0
    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

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

4

1 回答 1

0
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')

通过在用户代理之前添加用户代理解决。 您可以在此处查看更多浏览器代理。只需旋转然后使用随机。

于 2021-09-28T19:27:20.490 回答