add_argument for user agent cuts off at first space

64 views
Skip to first unread message

Jay Pinho

unread,
Feb 18, 2018, 10:12:21 PM2/18/18
to Selenium Users

OS: macOS High Sierra

Selenium Version: 3.9.0

Browser: Chrome

Browser Version: 64.0.3282.167 (Official Build) (64-bit)


I'm using Ruby and the Selenium WebDriver to view a webpage using headless Chrome.


I need to change the user agent for my project. On my local computer, I can easily do this via the add_argument method in Selenium::WebDriver::Chrome::Options:


options = Selenium::WebDriver::Chrome::Options.new()
options.add_argument('--headless')
options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36')
driver = Selenium::WebDriver.for(:chrome, options: options)
puts driver.execute_script('return navigator.userAgent')


This (correctly) returns:

 

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36


However, when I push this code to my Heroku app and run the same code*, suddenly here's what gets returned:


Mozilla/5.0


I have spent upwards of 4 hours trying all manner of fixes. Pretty early on I realized this was probably some kind of escaping issue, so I looked up how to escape spaces and (I think) have tried absolutely everything: backslashes, double-backslashes, quotes, double-quotes, etc. None of it has worked. For example, I tried:


options.add_argument('--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36"')


Which results in:


"Mozilla/5.0


As far as I can tell, absolutely any user agent I pass as an option will get cut off at the first space.


Am I missing something obvious here?


Thank you!


*The only change I make to the code running on Heroku is that I have to slightly update the first line of code above to point the Heroku app to the Chrome binary:


options = Selenium::WebDriver::Chrome::Options.new(binary: ENV['GOOGLE_CHROME_SHIM'])

Jay Pinho

unread,
Feb 20, 2018, 6:36:01 PM2/20/18
to Selenium Users

Wish I could take credit for this, but I actually just got the answer in a Chromedriver forum: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2276#c7.

In short, the Heroku buildpack I've been using had a bug that prevented arguments with spaces from being parsed correctly.

This line...

exec \$HOME/.apt/opt/google/$BIN --headless --no-sandbox --disable-gpu --remote-debugging-port=9222 \$@

...should have looked like this:

exec \$HOME/.apt/opt/google/$BIN --headless --no-sandbox --disable-gpu --remote-debugging-port=9222 "\$@"

I've since filed a PR that will fix the issue.

In the meantime, anyone can use my forked version of the buildpack, which I've verified parses the user agent correctly.

Reply all
Reply to author
Forward
0 new messages