Spoof navigator properties in chrome selenium webdriver

3.2k views Asked by At

I am trying to automate some tasks on a website using chrome selenium. I spoof the navigator.webdriver based on this post. How to spoof other properties of navigator like: deviceMemory, hardwareConcurrency, maxTouchPoints? I am using Selenium in C#.

Any help is much appreciated

2

There are 2 answers

0
petrosmm On BEST ANSWER
    var parameters = new Dictionary<string, object>
    {
        ["source"] = "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"
    };
    var driver = new ChromeDriver(@"c:\bin\", options);
    driver.ExecuteChromeCommand("Page.addScriptToEvaluateOnNewDocument", parameters);

Answer is tested by me and inspired by https://stackoverflow.com/a/60405966/3997521.

1
pguardiario On

You can set those before script execution similarly with cdp command. I'm not sure what it looks like in c# but in python:

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'deviceMemory', {
      get: () => 99
    })
  """
})