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地址。
我已经搜索了几乎所有的问题在OperaDriver上,以及在其他网站上。在任何地方似乎都没有任何与此相关的文件或任何其他问题。
离我最近的是github上的此特性请求。OP说,通过使用OperaOptions加载自定义配置文件,他能够使它工作。在链接中发布的代码是
OperaOptions operaOptions = new OperaOptions();
operaOptions.addArguments("user-data-dir", "~/Library/Application Support/com.operasoftware.Opera");
driver = new OperaDriver(operaOptions);我试着在python上做这件事,但没有结果。如果有任何问题,我使用Ubuntu16.04,OperaDriver是从官方github页面下载的。Python是3.6.7,Opera是57.0.3098.116 for Ubuntu 16.04 LTS (x86_64; Unity)。
发布于 2019-03-17 02:17:14
您正在尝试使用OperaOptions而不是ChromeOptions,来自opera/selenium.webdriver.opera.webdriver.html
选项:这是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,并检查路径下的配置文件行。
在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()发布于 2019-03-17 10:23:08
@Dan给出了一个很好的答案,并允许您在不需要任何手动干预的情况下启用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地址。所以这也能起作用。
Note
我试着用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>我无法自动单击该部件,但工作方式不同。如果我能这样做的话,我会更新这个答案。
发布于 2020-12-15 07:52:47
虽然这并不是一种在代码中这样做的方法,但它对我有效,我希望它能帮助您解决问题。打开完整的浏览器设置,从左侧选择Advanced下拉,然后单击Features。你应该看到一个按钮,上面写着Connect to VPN when starting browser。一旦打开它,每次在Opera中使用selenium时,您都会使用VPN浏览。
https://stackoverflow.com/questions/55130791
复制相似问题