[Ruby] Access with measures of navigator.webdriver with Selenium

1 minute read

enable-automation or useAutomationExtension didn’t work as expected

I want to scrape for some reason, but it was an NG site if the browser was navigator.webdriver=true. I want to correspond. It seems that I can do it. Reference: navigator.webdriver=true will tell you that it is a robot. Is there a way around that? puppeteer can do it But I’m using Selenium, I’ve never used puppeteer, and I can’t do it somehow. ..

Settings when it doesn’t move

Apparently I found some articles that seem to work with Selenium.

Reference 1: https://stackoverflow.com/questions/53039551/selenium-webdriver-modifying-navigator-webdriver-flag-to-prevent-selenium-detec

Reference 2: https://help.applitools.com/hc/en-us/articles/360007189411–Chrome-is-being-controlled-by-automated-test-software-notification

I tried it.

    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
      accept_insecure_certs: true,
      chromeOptions: {
        args: [
          '-window-size=1920,1080',
          '--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
        ],
        excludeSwitches: ['enable-automation'], # Added
        useAutomationExtension: false # add
      }
    )
    driver = Selenium::WebDriver.for(
      :remote,
      url:'http://chrome:4444/wd/hub',
      desired_capabilities: capabilities,
      http_client: Selenium::WebDriver::Remote::Http::Default.new
    )

Even if you actually move it, true will come back. It was no good.

driver.execute_script('return navigator.webdriver')
>>> true

Working settings

Removed enable-automation and useAutomationExtension. Set the driver as before.

    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
      accept_insecure_certs: true,
      chromeOptions: {
        args: [
          '-window-size=1920,1080',
          '--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
        ],
      }
    )
    driver = Selenium::WebDriver.for(
      :remote,
      url:'http://chrome:4444/wd/hub',
      desired_capabilities: capabilities,
      http_client: Selenium::WebDriver::Remote::Http::Default.new
    )

# Add below
    driver.execute_script('const newProto = navigator.__proto__;delete newProto.webdriver;navigator.__proto__ = newProto;')

If you actually move it, undefined will come back. Works similar to puppeteer. Was good.

driver.execute_script('return navigator.webdriver')
>>> undefined