. Advertisement .
..3..
. Advertisement .
..4..
When you try to do anything automated in the tool, the following error appears: “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java“. Don’t worry too much. This post will support you in resolving the error with the best solution.
When Will The Error “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java” Occur?
You received the following error when you ran your code.
Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
The Effective Methods For The Error “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java”
Solution 1: Add this option argument
To solve the error “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java”, you need to add this option argument as the code below to remove all traces of the webdriver.
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
Your error must now be fixed.
Solution 2: Use this simple flag
In this method, you can use this simple flag to handle your problem. Look at below:
--disable-blink-features=AutomationControlled
Solution 3: Follow these steps
A nice approach would include any/all of the following steps to prevent Selenium driven WebDriver from being detected:
First, you need to include the argument in your program:
options = webdriver.ChromeOptions()
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.website.com")
The next step, let’s use the execute cdp cmd() command, rotate the user-agent as follows:
#Setting up Chrome/83.0.4103.53 as useragent driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})
Then, you have to set the navigator for webdriver property value to undefined.
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
After that, exclude the group of enable-automation switches.
options.add_experimental_option("excludeSwitches", ["enable-automation"])
Finally, let’s turn off the use AutomationExtension by the following commnad:
options.add_experimental_option('useAutomationExtension', False)
After doing these steps, your error will be completely resolved and your program will run well without any errors. Let’s try it.
Solution 4: Thanks to the availability of the execute cdp cmd
Excepting the above solutions, there is another solution for you to solve the “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java” error. This method is based on the availability of the execute cdp cmd().
execute cdp cmd()
: You may now simply run Google-Chrome-DevTools commands by using Selenium thanks to the availability of the execute cdp cmd(cmd, cmd args)
command. You can change the navigator.webdriver easily to avoid detecting Selenium by using this feature.
This methood is very simple, right? However, it has enormous efficiency. Let’s do it to get your desired results.
Conclusion
Java is a general-purpose and object-oriented programming language developed for distributed environments and consumer electronics. This programming language is platform-independent, which means it is not restricted to any specific hardware or operating system. Those simple approaches presented above are a simple solution to the aforementioned major error: “Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection in Java“. Please leave all your concerns in the comments section if you have any further questions. I wish you all an effective day with your tool.
Read more
Leave a comment