'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:\Programming\Python\proxyTest.py", line 20, in <module>
proxyManage()
File "D:\Programming\Python\proxyTest.py", line 6, in proxyManage
proxyChange("test.com", "8000", "user1", "password1")
File "D:\Programming\Python\proxyTest.py", line 16, in proxyChange
browser = webdriver.Chrome(PATH, options=options)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\seleniumwire\webdriver\browser.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.
Solution 1:[1]
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.
Solution 2:[2]
Selenium is not suitable for auth.Its just web UI automation please check SeleniumHQ site
Two Factor Authentication shortly know as 2FA is a authorization mechanism where One Time Password(OTP) is generated using “Authenticator” mobile apps such as “Google Authenticator”, “Microsoft Authenticator” etc., or by SMS, e-mail to authenticate. Automating this seamlessly and consistently is a big challenge in Selenium. There are some ways to automate this process. But that will be another layer on top of our Selenium tests and not secured as well. So, you can avoid automating 2FA.
There are few options to get around 2FA checks:
- Disable 2FA for certain Users in the test environment, so that you can use those user credentials in the automation.
- Disable 2FA in your test environment.
- Disable 2FA if you login from certain IPs. That way we can configure our test machine IPs to avoid this.
Solution 3:[3]
Change
browser = webdriver.Chrome(PATH, options=options)
to
browser = webdriver.Chrome(PATH, seleniumwire_options=options)
works for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jackblk |
| Solution 2 | Justin Lambert |
| Solution 3 | user348484 |
