0

I'm trying to set the user-agent parameter in Selenium::Chrome, but am not finding the correct way. Has anyone successfully done this?

my $driver = Selenium::Chrome->new(
    'user-agent' => 'TEST',
    '--user-agent' => 'TEST',
    'chromeOptions' => {
        prefs => {
            'user-agent' => 'TEST',
            '--user-agent' => 'TEST',
        },
    },
    extra_capabilities => { 
        'chromeOptions' => {
            prefs => {
                'user-agent' => 'TEST',
                '--user-agent' => 'TEST',
            },
        },
        'goog:chromeOptions' => {
            prefs => {
                'download.default_directory' => '/tmp',
                'user-agent' => 'TEST',
                '--user-agent' => 'TEST'
            },
            args => [ 'headless' ]
        }
    }
);
lschult2
  • 588
  • 2
  • 5
  • 16
  • Based on the documentation [custom_args](https://metacpan.org/pod/Selenium::CanStartBinary#custom_args) should be used with `--user-agent`. – Steffen Ullrich Dec 21 '21 at 19:25

1 Answers1

1

user-agent can be set through the custom_args argument.

To set the user-agent parameter in Selenium::Chrome you can use the following solution:

my $chrome = Selenium::Chrome->new(
    custom_args => "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
);

PS: Ideally, you should pass a valid user-agent following What is my user agent?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Ah, we're getting close. Using that, I get the following error: `sh: -c: line 0: syntax error near unexpected token ``('` `sh: -c: line 0: ``"/usr/local/bin/chromedriver" --port=57511 --url-base=wd/hub --user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 > /dev/null 2>&1 &'` so it looks like it's not properly escaped. I put double quotes around the string, and the error went away. But the user-agent sent did not change. – lschult2 Dec 22 '21 at 01:46
  • It looks like chromedriver doesn't accept --user-agent=XYZ – lschult2 Dec 22 '21 at 02:14
  • @lschult2 Pass an original UserAgent accessing the url https://www.whatismybrowser.com/detect/what-is-my-user-agent – undetected Selenium Dec 22 '21 at 05:17
  • @lschult2 If you have any query feel free to ask me in [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Dec 22 '21 at 13:20