Explanation of setting proxy IP method in selenium

Source: Internet
Author: User

Set proxy IP in Firefox

Method_1

From selenium import webdriverprofile = Webdriver. Firefoxprofile () profile.set_preference (' Network.proxy.type ', 1) profile.set_preference (' Network.proxy.http ', ' 127.0.0.1 ') profile.set_preference (' Network.proxy.http_port ', 17890)  # intprofile.update_preferences () Driver = Webdriver. Firefox (firefox_profile=profile) driver.get (' HTTP://HTTPBIN.ORG/IP ')

Method_2

From selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy Import Proxytypeproxy = Proxy (    {        # ' Proxytype ': proxytype.manual,  # Use no all        ' httpproxy ': get_proxy_ip_ Port ()    }) Driver = Webdriver. Firefox (proxy=proxy) driver.get (' HTTP://HTTPBIN.ORG/IP ')

Set proxy IP in PHANTOMJS

Method One: Too not elegant (or look at the method two bar)

In the PHANTOMJS can not be like the above Firefox method2 passed into proxy
Phantomjs and Firefox are inherited from Webdriver, The parent class Webdriver can pass in the proxy
Phantomjs when initializing the Webdriver without a proxy parameter
so you can change the source of Phantomjs class like the same, you can pass in the proxy parameter in PHANTOMJS

# Note Authorization # Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreements.  See the NOTICE file# distributed and this work for additional information# regarding copyright ownership.  The SFC licenses this file# to you under the Apache License, Version 2.0 (the# "License"); You are not a use of this file except in compliance# with the License.  Obtain a copy of the License at##   http://www.apache.org/licenses/LICENSE-2.0## unless required by applicable l AW or agreed to writing,# software distributed under the License are distributed on an# "as is" BASIS, without Warrantie S or CONDITIONS of any# KIND, either express OR implied.  See the License for the# specific language governing permissions and limitations# under the License.

Here's an example.

From selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy Import Proxytypeproxy = Proxy (    {        ' proxytype ': proxytype.manual,        ' httpproxy ': Get_proxy_ip_port ()    }) Driver = Webdriver. PHANTOMJS (    executable_path= "/path/of/phantomjs",    proxy=proxy    ) driver.get (' HTTP://HTTPBIN.ORG/IP ') Print Driver.page_sourcedriver.close ()

Method Two:

From selenium import webdriverfrom selenium.webdriver.common.proxy import Proxyfrom selenium.webdriver.common.proxy Import proxytypeproxy = Proxy (    {        ' proxytype ': proxytype.manual,        ' httpproxy ': ' Ip:port '  # proxy IP and port    }) # Create a new "desired skill", haha desired_capabilities = DesiredCapabilities.PHANTOMJS.copy () # Add the proxy IP to the skill proxy.add_to_capabilities ( desired_capabilities) Driver = Webdriver. PHANTOMJS (    executable_path= "/path/of/phantomjs",    desired_capabilities=desired_capabilities    ) Driver.get (' http://httpbin.org/ip ') print driver.page_sourcedriver.close ()

Method Three (dynamic switch IP):

from Selenium import webdriverfrom selenium.webdriver.common.proxy Import        Proxyfrom selenium.webdriver.common.proxy Import proxytypeproxy = Proxy ({' Proxytype ': proxytype.manual, ' Httpproxy ': ' Ip:port ' # proxy IP and Port} # Create a new "desired skill", haha desired_capabilities = DesiredCapabilities.PHANTOMJS.copy () # Add proxy IP into the skill proxy.add_to_capabilities (desired_capabilities) driver = Webdriver. Phantomjs (executable_path= "/path/of/phantomjs", desired_capabilities=desired_capabilities) # Test Driver.get (' http        ://httpbin.org/ip ') print driver.page_source# now start switching ip# and create a new Ipproxy = Proxy ({' Proxytype ': proxytype.manual, ' Httpproxy ': ' Ip:port ' # proxy IP and Port} # Create a new "desired skill", () Desired_capabilities = DesiredCapabilities.PHANTOMJS.copy () # Proxy IP join to skill proxy.add_to_capabilities (desired_capabilities) # Create a new session and pass the skill to Driver.start_session (desired_ Capabilities) Driver.get (' http://httpbin.org/ip ') print driver.page_sourcedriver.close () 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.