'How to set User-Agent Client Hint sec-ch-ua in Selenium Python
I ran this code to set User-Agent Client Hint sec-ch-ua in Selenium Python but it still did not change.
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
sec_ch_ua = '" Not A;Brand";v="99", "Chromium";v="88", "Google Chrome";v="88"' # for example
options = Options()
options.add_argument(f'--sec-ch-ua={sec_ch_ua}')
driver = Chrome("chromedriver.exe", options=options)
driver.get("https://user-agent-client-hints.glitch.me/")
Solution 1:[1]
Chromium's GREASEing strategy
User agents' brands containing more than a single entry could encourage standardized processing of the brands list. By randomly including additional, intentionally incorrect, comma-separated entries with arbitrary ordering, they would reduce the chance that we ossify on a few required strings.
A couple of examples:
To avoid sites from barring unknown browsers from their allow lists, Chrome could send a UA set that includes an non-existent browser, and which varies once in a while.
"Chrome"; v="73", "(Not;Browser"; v="12"To enable equivalence classes based on Chromium versions, Chrome could add the rendering engine and its version to that.
"Chrome"; v="73", "(Not;Browser"; v="12", "Chromium"; v="73"To encourage sites to rely on equivalence classes based on Chromium versions rather than exact UA sniffing, Chrome might remove itself from the set entirely.
"(Not;Browser"; v="12", Chromium"; v="73"Browsers based on Chromium may use a similar UA string, but use their own brand as part of the set, enabling sites to count them.
"Chrome"; v="73", "Xwebs mega"; v="60", "Chromium"; v="73", "(Not;Browser"; v="12"
However, GREASEing have some chevets as follows:
- User agents MUST include more than a single value in brands, where one of these values is an arbitrary value.
- The value order in brands MUST change over time to prevent receivers of the header from relying on certain values being in certain locations in the list.
- When choosing GREASE strategies, user agents SHOULD keep caching variance and analytics use cases in mind and minimize variance among identical user agent versions.
- One approach to minimize variance for caching and analytics could be to determine the GREASE parts of the UA set at build time, and keep them identical throughout the lifetime of the user agent's significant version.
This usecase
As per the list in List of Chromium Command Line Switches I don't find the specific option being listed and still may not be available to be tweaked for your tests.
However you can try a few other values as follows:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
sec_ch_ua = '"Examplary Browser"; v="73", ";Not?A.Brand"; v="27"' # for example
options = Options()
options.add_argument(f'--sec-ch-ua={sec_ch_ua}')
driver = Chrome("chromedriver.exe", options=options)
driver.get("https://user-agent-client-hints.glitch.me/")
tl; dr
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | undetected Selenium |
