How to enable built-in VPN in OperaDriver?
Asked Answered
V

3

14

The opera browser has a built-in VPN which allows you to hide your IP while browsing. My question is can the VPN be turned on while using OperaDriver with selenium in python?

Attempt and problem in detail:

I have this script that goes to a website to display my IP address.

from selenium import webdriver
from selenium.webdriver.opera.options import Options
from time import sleep
driver = webdriver.Opera(executable_path=r'/path/to/operadriver')
driver.get('https://whatismyipaddress.com')
sleep(10)
driver.quit() 

When I go to this site on the opera browser with VPN enabled, my IP is masked and some other IP address is shown. But my script opens up the browser to display my real IP address.

I have searched almost all questions on OperaDriver on SO as well as on other sites. There seems to be absolutely no documentation or any other questions related to this anywhere.

The closest I got was this feature request on github. The OP says that he was able to make it work by using OperaOptions to load a custom profile. The code posted in the link is

OperaOptions operaOptions = new OperaOptions();
operaOptions.addArguments("user-data-dir", "~/Library/Application Support/com.operasoftware.Opera");
driver = new OperaDriver(operaOptions);

I tried to do this in python and nothing worked out. If it is of any concern I use Ubuntu 16.04, and OperaDriver is downloaded from the official github page. Python version is 3.6.7 and Opera version is 57.0.3098.116 for Ubuntu 16.04 LTS (x86_64; Unity).

Vandavandal answered 12/3, 2019 at 21:18 Comment(3)
I think what that person did is enable VPN from the GUI and the setting got saved in the active profile. The they always apply that specific profile when starting Opera programmatically. Similar idea to the one explained hereRagman
@Ragman This does not seem to be the case for me. When starting OperaDriver seems to always start with a new profile for me.Vandavandal
For c# users I think this link has an accepted answer ru.#711783Vandavandal
P
11

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()
Parachronism answered 17/3, 2019 at 2:17 Comment(1)
Thank you for the excellent answer. I see that you have pointed to the config folder. If I may ask for the benefit of Windows users, Do you know where the location of that folder will be in Windows? Is it similar to ~/Library/Application Support/com.operasoftware.Opera which i have mentioned in my question?Vandavandal
V
3

@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.

Vandavandal answered 17/3, 2019 at 10:23 Comment(0)
M
0

While this isn't exactly a way to do it in code, it worked for me and I hope it helps you out. Open the full browser settings, choose the Advanced drop down from the left, and click on Features. You should see a button that says Connect to VPN when starting browser. Once you turn it on, every time you use selenium with Opera, you will browse with a VPN.

Manofwar answered 15/12, 2020 at 7:52 Comment(1)
It doesn't work because the driver opens a new development window and your all defined it will be undefined.Totally

© 2022 - 2024 — McMap. All rights reserved.