Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

python - How to enable built-in VPN in OperaDriver?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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\ProgramsOpera\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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...