4

In Python, I could easily change the browser "navigator.webdriver" property to false, when using the local chromedriver with my local Chrome browser.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(chrome_options=options)
driver.get([some url])

After the above, in the Chrome browser console, navigator.webdriver would show "false".

But I do not know how to translate the above to Perl. The following code would still leave the navigator.webdriver as "true". So how do I implement the Python code above in Perl? Is it possible (ideally without using the remote stand-alone selenium server)?

use strict;
use warnings;
use Selenium::Chrome;
my $driver = Selenium::Chrome->new(
   custom_args => '--disable-blink-features=AutomationControlled' );
$driver->get([some url]);

Any help would be greatly appreciated!

Shang Zhang
  • 269
  • 1
  • 8
  • "The following code does not work." Could you be more specific about how it doesn't work? Do you get an error? Please [edit] your question with the details. – Nathan Mills Mar 17 '22 at 22:25
  • After the Perl code, "in the Chrome browser console, navigator.webdriver would show TRUE", unlike the Python code which successfully turns navigator.webdriver to FALSE. So how do I do the same in Perl? – Shang Zhang Mar 17 '22 at 23:02

1 Answers1

5

Need to use extra_capabilities with goog:chromeOptions

my $drv = Selenium::Chrome->new(
     'extra_capabilities' => {
         'goog:chromeOptions' => {
             prefs => { ... },
             args => [ 
                 'window-position=960,10', 'window-size=950,1180', # etc   
                 'disable-blink-features=AutomationControlled'
             ]
         }
     }
);

(I don't know how disable-blink-features relates to that "navigator.webdriver")

For the list of attributes available in the constructor, along with the extra_capabilities, see Selenium::Remote::Driver, from which Selenium::Chrome inherits.

For Chrome-specific capabilities see "Recognized capabilities" in chromedriver, and more generally see Selenium documentation and W3C WebDriver standard.


Specifically

zdim
  • 64,580
  • 5
  • 52
  • 81
  • Thanks so much! I don't really know why disable-blink-features affects navigator.webdriver either. Saw it on the Internet, w/ Python examples. But I need it in Perl. (Sad that one could hardly find any Perl tutorials/examples these days...) Ideally, I would also want navigator.webdriver=false for Selenium::Firefox. It seems that cannot be done, unless marionette=0 which pretty much makes Selenium::Firefox useless. Any thoughts on how to get navigator.webdriver=false for Selenium::Firefox? – Shang Zhang Mar 18 '22 at 01:31
  • @ShangZhang How about `my $drv = Selenium::Firefox->new(marionette_enabled => 1, extra_capabilities => { 'moz:firefoxOptions' => { args => [ ... ] }});` ? I don't know what exactly that `navigator.webdriver` is, perhaps that can be set to `false` directly? There is quite a bit more that can be set up in the constructor. I'll add some links when i get to it. – zdim Mar 18 '22 at 02:32
  • @ShangZhang Added linkswhich hopefully help some with this issue, largely undocumented in Perl – zdim Mar 18 '22 at 09:23
  • Thanks @zdim. I tried extra_capabilities => { 'moz:firefoxOptions' => { args => [ disable-blink-features=AutomationControlled ] }}) but failed. This is to be expected, as disable-blink-features is Chrome specific? navigator.webdriver is part of W3C WebDriver interface (https://www.w3.org/TR/webdriver/). I don't have the first idea on how to set it to false directly. That would be nice though. – Shang Zhang Mar 19 '22 at 04:05