6

How do I mimic or set plugins length for Selenium chrome headless? I am following this guide

I cannot find anything on doing this through Chrome options.

The reason I want this is I want to make chrome headless as undetectable as possible.

Try the following. Plugin length is still an issue.

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.add_argument("--headless");
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3240.0 Safari/537.36'
options.add_argument(f'user-agent={user_agent}')
options.add_argument("--lang=en-US,en");

driver = webdriver.Chrome(chrome_options=options)

driver.get('https://intoli.com/blog/making-chrome-headless-undetectable/chrome-headless-test.html')
driver.save_screenshot("screenshot5.png")

driver.close()

As you can see below, plugin length is still quite detectable.

enter image description here

Is this possible or just a limitation of selenium python?

Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
  • Chrome headless doesn't allow extensions, so thats what you are seeing here. It is a limitation of chrome headless itself. Selenium can't do anything about it – Tarun Lalwani Dec 24 '17 at 12:05
  • @TarunLalwani I see. I believe there is a way to inject javascript through a file to change this but not too sure how I go about. –  Dec 24 '17 at 21:53
  • 2
    if you are trying to inject Js see how I did it in a different content here: [this link][1] [1]: https://stackoverflow.com/a/45194334/8179099 – Moshe Slavin Dec 28 '17 at 11:24
  • 1
    @MosheSlavin Quite interesting though does not clarify how this can be implemented with JS. Tarun is correct, this is limitation of chrome headless –  Dec 30 '17 at 14:04

1 Answers1

1
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
  "source": "
     Object.defineProperty(navigator, 'plugins', {
       get: () => [1, 2, 3, 4, 5]
     });
"})

This is the solution for plugins.length

The author mentioned solution in the post. I confirmed it works perfectly.

Francesco Tagliani
  • 109
  • 1
  • 1
  • 10
  • is it actual in headless mode? I set it and after i check number of plugins and this: "return navigator.plugins.length;" return me 0 – Pawel W Jul 28 '22 at 12:42