Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Conceal WebDriver in Geckodriver from BotD in Java?

I followed this post on Stackoverflow to disable Firefox WebDriver detection.

Launch Geckodriver:

System.setProperty("webdriver.gecko.driver", geckdriverExecutableFilePath);



File firefoxProfileFile = new File(fullPathOfFirefoxInstallationFolder);



FirefoxProfile firefoxProfile = null;

        
 try {
    firefoxProfile = new FirefoxProfile(firefoxProfileFile);
     } catch (Exception e) {

    e.printStackTrace();
     }

I disabled WebDriver:

WebDriver Disabled

FirefoxOptions firefoxOptions = new FirefoxOptions();

firefoxOptions.setProfile(firefoxProfile);

// Disables WebRTC
firefoxProfile.setPreference("media.peerconnection.enabled", false);

I disabled Automation Extensions:

Automation Extension Disabled

// Disables Automation Extension
firefoxProfile.setPreference("useAutomationExtension", false);

I added Proxy:

    DesiredCapabilities dc = DesiredCapabilities.firefox();
    Proxy proxy = new Proxy();
    proxy.setHttpProxy(ipAddress + ":" + port);
    proxy.setFtpProxy(ipAddress + ":" + port);
    proxy.setSslProxy(ipAddress + ":" + port);

   dc.setCapability(CapabilityType.PROXY, proxy);




   firefoxOptions.merge(dc);

   driver = new FirefoxDriver(firefoxOptions);

Yet BotD still detects my browser as being controlled by automation tool.

BotD Detection

How can I solve this?

like image 778
Morgan Osker Avatar asked Nov 14 '21 00:11

Morgan Osker


People also ask

Can a website detect when you are using Selenium with GeckoDriver?

Your question is, "Can a website detect when you are using selenium with geckodriver?" The answer is yes, Basically the way the selenium detection works, is that they test for pre-defined javascript variables which appear when running with selenium.

How do I turn off WebDriver?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.

How do you write GeckoDriver in Selenium?

Steps to Add a Path in System's PATH Environmental Variable Click on the Environment Variables button. From System Variables select PATH. Click on the Edit button. Paste the path of the GeckoDriver file.

How to use geckodriver in Selenium WebDriver?

How to use GeckoDriver in Selenium Project? 1 Let us consider that you have the latest version of Selenium WebDriver and the Firefox browser. 2 Then download the GeckoDriver from here. Later, choose the version which is suitable for your computer. More ...

How to add the path of the geckodriver in the system?

We can follow the steps mentioned below to add the path of the GeckoDriver in the System's PATH variable: Firstly, open properties by right-clicking on This PC. Secondly, open Advanced System Settings and click on Environment Variables. Thirdly, under the System variables, select Path and click on Edit.

Why do we need to initialize geckodriver before creating the WebDriver?

We need to initialize it before creating the instance of the WebDriver explicitly. The path of the GeckoDriver executable file should be accessible to the FirefoxDriver, so as when the User creates an instance of the WebDriver using the FirefoxDriver, it should be able to find the path of the GeckoDriver executable file.

What is the use of gecko driver in Firefox?

GeckoDriver is a proxy to communicate with Gecko-based browsers ( E.g. Firefox). Firefox (version47 and above) has done some changes, which has led to prevention of supporting third-party drivers to interact directly with the browsers. This is the primary reason for which we need to use the GeckoDriver.


Video Answer


1 Answers

When using Selenium driven GeckoDriver initiated firefox Browsing Context

The webdriver-active flag is set to true when the user agent is under remote control. It is initially false.

webdriver-active flag

where, webdriver returns true if webdriver-active flag is set, false otherwise.

As:

navigator.webdriver Defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, for example so that alternate code paths can be triggered during automation.

Further @whimboo in his comments confirmed:

This implementation have to be conformant to this requirement. As such we will not provide a way to circumvent that.


Conclusion

So, the bottom line is:

Selenium identifies itself

and there is no way to conceal the fact that the browser is WebDriver driven.


Recommendations

However some pundits have suggested some different approaches which can conceal the fact that the Mozilla Firefox browser is WebDriver controled through the usage of Firefox Profiles and Proxies as follows:

selenium4 compatible python code

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference('network.proxy.socks_remote_dns', False)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()

Potential Solution

A potential solution would be to use the tor browser as follows:

selenium4 compatible python code

from selenium.webdriver import Firefox  
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
import os

torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile_path = r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default'
firefox_options=Options()
firefox_options.set_preference('profile', profile_path)
firefox_options.set_preference('network.proxy.type', 1)
firefox_options.set_preference('network.proxy.socks', '127.0.0.1')
firefox_options.set_preference('network.proxy.socks_port', 9050)
firefox_options.set_preference("network.proxy.socks_remote_dns", False)
firefox_options.binary_location = r'C:\Users\username\Desktop\Tor Browser\Browser\firefox.exe'
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = webdriver.Firefox(service=service, options=firefox_options)
driver.get("https://www.tiktok.com/")

References

You can find a couple of relevant detailed discussions in

  • How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python
  • How to connect to Tor browser using Python
  • How to use Tor with Chrome browser through Selenium
like image 87
undetected Selenium Avatar answered Nov 05 '22 00:11

undetected Selenium