如何使用不同的代理selen打开每个窗口

时间:2017-09-19 21:05:36

标签: python google-chrome selenium selenium-webdriver webdriver

所以我有这个脚本打开'link'数组中的每个项目。但是,我希望每个窗口都使用不同的ip:port:user:pass proxies打开。

到目前为止,这是我的代码。

from selenium import webdriver

def main():

# link = "http://www.google.com"
link = ["www.google.com", "www.wikipedia.com"]


windows = len(link)
DRIVERS = []
position = [0,0]
count = 0
for i in range(0,windows):
    if count == 1000:
        count = 0
        position[0] += 300

    driver = webdriver.Chrome(executable_path="drivers/chromedriver")

    driver.set_window_size(300,200)
    driver.get(link[i])
    DRIVERS.append(driver)
    driver.set_window_position(position[0], position[1]+count)
    count += 200

exit = input("exit? ")
for eachWindow in DRIVERS:
    eachWindow.quit()


if __name__ == '__main__':
    main()

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

来自Selenium官方网站的回答: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy

for i in range(0,windows):
    PROXY = "localhost:8080"

    # Create a copy of desired capabilities object.
    desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()
    # Change the proxy properties of that copy.
    desired_capabilities['proxy'] = {
        "httpProxy":PROXY,
        "ftpProxy":PROXY,
        "sslProxy":PROXY,
        "noProxy":None,
        "proxyType":"MANUAL",
        "class":"org.openqa.selenium.Proxy",
        "autodetect":False
    }

    # you have to use remote, otherwise you'll have to code it yourself in python to 
    # dynamically changing the system proxy preferences
    driver = webdriver.Chrome(executable_path="drivers/chromedriver",
             desired_capabilities=desired_capabilities)

如果您的代理需要身份验证并接受Basic HTTP Auth,您可以尝试使用格式的代理:

http://user:password@proxyurl