17

Using Selenium and python, I can do this with Chrome webdriver:

options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options = options)

but I can't find a similar attribute for Firefox's webdriver options. Does one exist?

lucas
  • 1,485
  • 13
  • 22

2 Answers2

11

Firefox uses different flags. I am not sure exactly what your aim is but I am assuming you are trying to avoid some website detecting that you are using selenium.

There are different methods to avoid websites detecting the use of Selenium.

1) The value of navigator.webdriver is set to true by default when using Selenium. This variable will be present in Chrome as well as Firefox. This variable should be set to "undefined" to avoid detection.

2) A proxy server can also be used to avoid detection.

3) Some websites are able to use the state of your browser to determine if you are using Selenium. You can set Selenium to use a custom browser profile to avoid this.

The code below uses all three of these approaches.

profile = webdriver.FirefoxProfile('C:\\Users\\You\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\something.default-release')

PROXY_HOST = "12.12.12.123"
PROXY_PORT = "1234"
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", PROXY_HOST)
profile.set_preference("network.proxy.http_port", int(PROXY_PORT))
profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX

driver = webdriver.Firefox(firefox_profile=profile, desired_capabilities=desired)
CST
  • 747
  • 1
  • 8
  • 12
  • 1
    Great, but does any of that actually work because navigator.webdriver is a read-only attribute, so anything that starts off with the advice to set that to something else isn't looking great. https://developer.mozilla.org/en-US/docs/Web/API/Navigator/webdriver – lucas Mar 11 '20 at 23:32
  • 3
    Yes the attribute is read-only. That means it cannot be **changed**, but it can be **initialised** to any value. Hence, the code is launching a browser with the attribute set to "undefined" instead of the default of "true". To demonstrate the difference, you can run JavaScript after launching the browser to change the attribute and it will not work. To test the code as I did before answering, you can run it and then manually open the dev console and type `navigator.webdriver` and hit enter. You will see it is now "undefined". – CST Mar 13 '20 at 18:56
  • I tested with Selenium WebDriver using the JavaScript / Node.js API by setting `dom.webdriver.enabled` to `false`. Unfortunately, I can still see in the web console that `navigator.webdriver` is set to `true`. Is there a workaround? By the way, how can setting `useAutomationExtension` to `false` be useful to avoid detection? – baptx Jul 19 '23 at 18:44
2

you may try:

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver import Firefox


profile = FirefoxProfile()
profile.set_preference('devtools.jsonview.enabled', False)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX

driver = Firefox(firefox_profile=profile, desired_capabilities=desired)
olli_kahn
  • 157
  • 6