Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

python - Implementing Selenium to use authentication proxies that change

I am trying to get selenium to use a proxy that will change at a certain point.

from seleniumwire import webdriver
def proxyManage():
    proxyChange("test.com", "8000", "user1", "password1")

def proxyChange(host, port, username, password):
    options = {
        'proxy': {
            'http': 'http://'+username+':'+password+'@'+host+':'+port, 
            'https': 'https://'+username+':'+password+'@'+host+':'+port,
        }
    }
    PATH = "D:/Programming/undetectable chrome/chromedriver.exe"
    browser = webdriver.Chrome(PATH, options=options)
    browser.get("https://whatismyipaddress.com/")
    

proxyManage()

So I import seleniumwire as I am unsure how normal selenium uses proxies. Now when I try to run the program to test on the website if it works I get an error below,

Traceback (most recent call last):
  File "D:ProgrammingPythonproxyTest.py", line 20, in <module>
    proxyManage()
  File "D:ProgrammingPythonproxyTest.py", line 6, in proxyManage
    proxyChange("test.com", "8000", "user1", "password1")
  File "D:ProgrammingPythonproxyTest.py", line 16, in proxyChange
    browser = webdriver.Chrome(PATH, options=options)
  File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagesseleniumwirewebdriverrowser.py", line 97, in __init__
    chrome_options.add_argument('proxy-bypass-list=<-loopback>')
AttributeError: 'dict' object has no attribute 'add_argument'

So first is there a way I can pass these arguments to proxyChange() without the error. The idea is to have a counter in proxyManage() and every time it runs it will move the next line in the proxy.txt file and then parse these arguments to proxyChange() and hopefully it will update without the program closing again? Would be nice to multithread this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try to construct it first before passing it to the dict:

http_proxy = f"'http://{username}:{password}@{host}:{port}"
https_proxy = f"'https://{username}:{password}@{host}:{port}"
options = {
        'proxy': {
            'http': http_proxy,
            'https': https_proxy
        }
    }

This only works for Python >=3.6. If you're using lower version, concentrate strings should work as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...