How to enable built-in VPN in OperaDriver?

@Dan-Dev has given an excellent answer and allows you to enable the VPN without any manual intervention.

I would like to share an alternative method that I was trying out in the meantime. This Requires a manual intervention to enable the VPN. Only consider this if the accepted answer is not working for you.

STEPS

  • Go to the opera privacy settings page at opera://settings/privacy first.
  • Give a sleep time to allow manual intervention.
  • Scroll Down and click on the Enable VPN Button.

enter image description here

  • Continue with the rest of your actions/logic.

Code:

from selenium import webdriver
from time import sleep
driver = webdriver.Opera(executable_path=r'path/to/operadriver')
driver.get('opera://settings/privacy')
sleep(30) #use this sleep to maually enable the VPN
#The rest of your logic goes below 
#I am just checking my address from a different url
driver.get('https://whatismyipaddress.com')
driver.quit() 

Result:

enter image description here

This is not my IP address. So this will work as well.

Note

I did try to click that button with selenium but was unsuccessful in my attempt. Viewing the page source using driver.page_source gave me something like this

<dom-module id="settings-startup-url-dialog" assetpath="on_startup_page/" css-build="shadow">
  <template>
    <style include="settings-shared" scope="settings-startup-url-dialog"></style>
    <cr-dialog id="dialog" close-text="Close">
      <div slot="title">[[dialogTitle_]]</div>
      <div slot="body">
        <cr-input id="url" label="Site URL" value="{{url_}}" on-input="validate_" spellcheck="false" maxlength="[[urlLimit_]]" invalid="[[hasError_(error_)]]" autofocus="" error-message="[[errorMessage_('Invalid URL',&#10;                'Please enter a shorter URL', error_)]]">
        </cr-input>
      </div>
      <div slot="button-container">
        <paper-button class="cancel-button" on-click="onCancelTap_" id="cancel">Cancel</paper-button>
        <paper-button id="actionButton" class="action-button" on-click="onActionButtonTap_">[[actionButtonText_]]</paper-button>
      </div>
    </cr-dialog>
  </template>
  </dom-module>

I was not able to automate that clicking part, but works otherwise. I will update this answer if I am able to do that.


You are trying to use OperaOptions not ChromeOptions, from https://seleniumhq.github.io/selenium/docs/api/py/webdriver_opera/selenium.webdriver.opera.webdriver.html

options: this takes an instance of ChromeOptions

As kaqqao says

"enable VPN from the GUI and the setting got saved in the active profile."

from selenium import webdriver
from time import sleep

# The profile where I enabled the VPN previously using the GUI.
opera_profile = '/home/dan/.config/opera' 
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + opera_profile)
driver = webdriver.Opera(options=options)
driver.get('https://whatismyipaddress.com')
sleep(10)
driver.quit()

Results:

First try
IPv6: 2001:67c:2660:425:2:0:0:3f8
IPv4: 77.111.247.26

Second try
IPv6: 2001:67c:2660:425:1a:0:0:1a0
IPv4: 77.111.247.66

Third try
IPv4: 77.111.247.133
IPv6: Not detected

Forth try
IPv6: 2001:67c:2660:425:1c:0:0:1fe
IPv4: 77.111.247.68

None of which are my IP and the VPN icon is showing next to the address bar.

UPDATED in response to question.

From https://techdows.com/2016/08/opera-profile-location.html

Simple way to know the profile path of Opera is just type about://about in address bar and check for the Profile line under paths.

On Windows 10 the code looks like this.

from selenium import webdriver
from time import sleep

# The profile where I enabled the VPN previously using the GUI.
opera_profile = r'C:\\Users\\dan\\AppData\\Roaming\\Opera Software\\Opera Stable' 
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + opera_profile)
options._binary_location = r'C:\\Users\\dan\\AppData\\Local\\Programs\Opera\\58.0.3135.114\\opera.exe'
driver = webdriver.Opera(executable_path=r'C:\\operadriver_win64\\operadriver.exe',options=options)
driver.get('https://whatismyipaddress.com')
sleep(10)
driver.quit()