Opera浏览器具有内置VPN,允许您在浏览时隐藏IP。我的问题是,在Python中使用OperaDriver和selenium时可以打开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加载自定义配置文件来使其工作。链接中发布的代码是
OperaOptions operaOptions = new OperaOptions();
operaOptions.addArguments("user-data-dir", "~/Library/Application Support/com.operasoftware.Opera");
driver = new OperaDriver(operaOptions);
我试图在python中做到这一点,没有任何结果。如果有任何问题我使用Ubuntu 16.04,并从official github page下载OperaDriver。 Python版本是3.6.7
,Opera版本是57.0.3098.116
的Ubuntu 16.04 LTS (x86_64; Unity)
。
您正在尝试使用来自https://seleniumhq.github.io/selenium/docs/api/py/webdriver_opera/selenium.webdriver.opera.webdriver.html的OperaOptions而非ChromeOptions
选项:这需要一个ChromeOptions实例
正如卡克乔所说
“从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()
结果:
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
这些都不是我的IP,VPN图标显示在地址栏旁边。
更新以回应问题。
来自https://techdows.com/2016/08/opera-profile-location.html
知道Opera的配置文件路径的简单方法就是在地址栏中键入about:// about,并检查路径下的Profile行。
在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隐私设置页面。码:
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', '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>
我无法自动点击该部分,但其他方式无效。如果我能够这样做,我会更新这个答案。