如何在 OperaDriver 中启用内置 VPN?

How to enable built-in VPN in OperaDriver?

opera 浏览器有一个内置的 VPN,可以让你在浏览时隐藏你的 IP。 我的问题是在 python 中使用带有 selenium 的 OperaDriver 时可以打开 VPN 吗?

详细尝试和问题:

我有这个脚本可以转到网站以显示我的 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() 

当我在启用了 VPN 的 Opera 浏览器上访问此站点时,我的 IP 被屏蔽并显示了一些其他 IP 地址。但是我的脚本会打开浏览器以显示我的真实 IP 地址。

我在 SO 以及其他网站上搜索了几乎所有关于 OperaDriver 的问题。似乎在任何地方都绝对没有与此相关的文档或任何其他问题。

我得到的最接近的是 this feature request on github。 OP 说他能够通过使用 OperaOptions 加载自定义配置文件来使其工作。 link 中发布的代码是

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

我尝试在 python 中执行此操作,但没有成功。如果有什么问题我用的是Ubuntu16.04,而OperaDriver是从official github page下载的。 Python 版本为 3.6.7,Opera 版本为 57.0.3098.116 for Ubuntu 16.04 LTS (x86_64; Unity)

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

options: this takes an instance of ChromeOptions

正如kaqqao所说

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

结果:

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

已更新 回答问题。

来自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.

在 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 给出了一个很好的答案,并允许您在没有任何手动干预的情况下启用 VPN。

我想分享一个我正在尝试的替代方法。这需要手动干预才能启用 VPN。仅当接受的答案不适合您时才考虑这个。

步骤

  • 首先转到 opera://settings/privacy 的 Opera 隐私设置页面。
  • 给一个睡眠时间允许人工干预。
  • 向下滚动并单击启用 VPN 按钮。

  • 继续 actions/logic。

代码:

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

结果:

这不是我的 IP 地址。所以这也会起作用。

备注

我确实尝试用 selenium 单击该按钮,但没有成功。使用 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>

我无法自动执行该点击部分,但可以正常工作。如果可以的话,我会更新这个答案。

虽然这不完全是一种用代码实现的方法,但它对我有用,我希望它能帮助你。打开完整的浏览器设置,选择左侧的 Advanced 下拉菜单,然后单击 Features。您应该会看到一个显示 Connect to VPN when starting browser 的按钮。一旦你打开它,每次你在 Opera 中使用 selenium 时,你将使用 VPN 浏览。