How to enable built-in VPN in OperaDriver?

前端 未结 3 1033
时光说笑
时光说笑 2020-12-16 20:14

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 pytho

相关标签:
3条回答
  • 2020-12-16 20:40

    @Dan-Dev has given an excellent answer and allows you to enable the VPN without any manual intervention.

    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. 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.
    • Give a sleep time to allow manual intervention.
    • Scroll Down and click on the Enable VPN Button.

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

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

    0 讨论(0)
  • 2020-12-16 20:45

    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\\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()
    
    0 讨论(0)
  • 2020-12-16 20:59

    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. You should see a button that says Connect to VPN when starting browser. Once you turn it on, every time you use selenium with Opera, you will browse with a VPN.

    0 讨论(0)
提交回复
热议问题