简体   繁体   English

如何在 OperaDriver 中启用内置 VPN?

[英]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.歌剧浏览器有一个内置的 VPN,允许您在浏览时隐藏您的 IP。 My question is can the VPN be turned on while using OperaDriver with selenium in python?我的问题是在 python 中使用带有 selenium 的 OperaDriver 时可以打开 VPN 吗?

Attempt and problem in detail:详细尝试和问题:

I have this script that goes to a website to display my IP address.我有这个脚本可以访问网站以显示我的 IP 地址。

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.当我在启用了 VPN 的 Opera 浏览器上访问此站点时,我的 IP 被屏蔽并显示了其他一些 IP 地址。 But my script opens up the browser to display my real IP address.但是我的脚本会打开浏览器以显示我的真实 IP 地址。

I have searched almost all questions on OperaDriver on SO as well as on other sites.我在 SO 以及其他网站上搜索了几乎所有关于 OperaDriver 的问题。 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 .我得到的最接近的是github 上的这个功能请求 The OP says that he was able to make it work by using OperaOptions to load a custom profile. OP 说他能够通过使用 OperaOptions 加载自定义配置文件来使其工作。 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.我尝试在 python 中执行此操作,但没有任何效果。 If it is of any concern I use Ubuntu 16.04, and OperaDriver is downloaded from the official github page .如果有任何问题,我使用 Ubuntu 16.04,OperaDriver 是从 官方 github 页面下载的。 Python version is 3.6.7 and Opera version is 57.0.3098.116 for Ubuntu 16.04 LTS (x86_64; Unity) .对于Ubuntu 16.04 LTS (x86_64; Unity) Python 版本为3.6.7 ,Opera 版本为57.0.3098.116

You are trying to use OperaOptions not ChromeOptions, from https://seleniumhq.github.io/selenium/docs/api/py/webdriver_opera/selenium.webdriver.opera.webdriver.html您正在尝试使用 OperaOptions 而不是 ChromeOptions,来自https://seleniumhq.github.io/selenium/docs/api/py/webdriver_opera/selenium.webdriver.opera.webdriver.html

options: this takes an instance of ChromeOptions选项:这需要一个 ChromeOptions 的实例

As kaqqao says正如卡卡所说

"enable VPN from the GUI and the setting got saved in the active profile." “从 GUI 启用 VPN,设置已保存在活动配置文件中。”

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.其中没有一个是我的 IP,VPN 图标显示在地址栏旁边。

UPDATED in response to question.更新以回答问题。

From https://techdows.com/2016/08/opera-profile-location.html来自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.了解 Opera 配置文件路径的简单方法是在地址栏中输入 about://about 并检查路径下的配置文件行。

On Windows 10 the code looks like this.在 Windows 10 上,代码如下所示。

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

@Dan-Dev has given an excellent answer and allows you to enable the VPN without any manual intervention. @Dan-Dev 给出了一个很好的答案,并允许您在没有任何手动干预的情况下启用 VPN。

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.这需要手动干预才能启用 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.首先转到opera://settings/privacy上的opera 隐私设置页面。
  • Give a sleep time to allow manual intervention.给予睡眠时间以允许手动干预。
  • Scroll Down and click on the Enable VPN Button.向下滚动并单击启用 VPN 按钮。

在此处输入图片说明

  • 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:结果:

在此处输入图片说明

This is not my IP address.这不是我的 IP 地址。 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使用driver.page_source查看页面源给了我这样的东西

<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.如果我能够做到这一点,我会更新这个答案。

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 .打开完整的浏览器设置,从左侧选择Advanced下拉菜单,然后单击Features You should see a button that says Connect to VPN when starting browser . Connect to VPN when starting browser ,您应该会看到一个显示“ Connect to VPN when starting browser的按钮。 Once you turn it on, every time you use selenium with Opera, you will browse with a VPN.打开它后,每次在 Opera 中使用 selenium 时,您都将使用 VPN 进行浏览。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM