Traffic not passing through mitmproxy when using Selenium in Pyhton - python

I have installed mitmproxy. It works fine when setting the proxy in my network preferences on my mac. I'd like to not have to set it up on my system but just for the traffic of a python script that uses Selenium, but it doesn't work:
from selenium import webdriver
from selenium.webdriver.common.proxy import *
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time
def main():
options = Options()
#options.accept_untrusted_certs = True
#options.add_argument("--headless")
myProxy = "localhost:8080"
options.proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': ''
})
driver = webdriver.Chrome(
ChromeDriverManager().install()
,options=options
)
driver.get('http://mitm.it')
time.sleep(20)
driver.quit()
main()
When the script opens http://mitm.it I get the following message:
"If you're seeing this it means your traffic is not passing through mitmproxy."
I guess the options are not passed correctly in the webdriver but I don't know why.

Related

open chrome tab with proxy using selenium python

capabilities = webdriver.DesiredCapabilities.CHROME
Proxy.add_to_capabilities(capabilities)
driver = webdriver.chrome('tmp/chromedriver', desired_capabilities=capabilities)
driver.get("https://www.google.com/")`
I'm trying to open a chrome tab with proxy (no authentication needed) but I got this error:
add_to_capabilities() missing 1 required positional argument: 'capabilities' error
Try following code as EX:
from selenium import webdriver
from selenium.webdriver.common.proxy import *
proxy_url = "127.0.0.1:9009"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': proxy_url,
'sslProxy': proxy_url,
'noProxy': ''})
capabilities = webdriver.DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get("http://example.org")

Selenium Python Proxy

I want to use proxy with selenium, I watched the video from youtube how to do it, but it doesn't work. This is my code:
import names
import time as t
import random
import requests
import pyautogui
import smsactivateru
import tkinter as tk
import sys
import socket
from selenium import webdriver
from selenium.webdriver.common.proxy import *
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from smsactivateru import Sms, SmsTypes, SmsService, GetBalance, GetFreeSlots, GetNumber
from fake_useragent import UserAgent
options = webdriver.FirefoxOptions()
# set useragent
ua = UserAgent()
options.set_preference("general.useragent.override",ua.random)
# set proxy
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
proxy = '91.214.31.234:8080'
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy
}
driver = webdriver.Firefox(options=options, proxy=proxy, capabilities=firefox_capabilities)
driver.get('https://2ip.ru')
t.sleep(5)
Why proxy doesn't work? Please help me. Kind people
The following code works for me in chrome.
PROXY = '91.214.31.234:8080'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(options=chrome_options)
chrome.get("https://www.icanhazip.com")
Try this for FireFox.
from selenium import webdriver
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy": PROXY,
"sslProxy": PROXY,
"proxyType": "MANUAL",
}
driver = webdriver.Firefox()
driver.get("https://www.icanhazip.com")

How do I set a proxy for chrome's webdriver in python

I have already tried:
'''
from selenium import webdriver
PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT
options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome('./chromedriver',options=options) #<----- I used './chromedriver' to set a PATH
chrome.get("http://whatismyipaddress.com")
'''
The above code is taken from how do i set proxy for chrome in python webdriver.
I am getting an error from google chrome saying ERR_NO_SUPPORTED_PROXIES, even when I am using public ones.
you can try this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT
capabilities = dict(DesiredCapabilities.CHROME)
capabilities['proxy'] = {'proxyType': 'MANUAL','httpProxy': '23.23.23.23:3128','ftpProxy': '23.23.23.23:3128','sslProxy': '23.23.23.23:3128','noProxy': '','class': "org.openqa.selenium.Proxy",'autodetect': False}
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--verbose')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-software-rasterizer')
chrome_options.add_argument('--headless')
chrome = webdriver.Chrome('./chromedriver',options=chrome_options) #<----- I used './chromedriver' to set a PATH
chrome.get("http://whatismyipaddress.com")

Using proxy with docker selenium python not working

I want to use proxy with username and password in my docker selenium container.
I tried every solution that I found on so and it doesn't work. I tried http proxy:
capabilities = DesiredCapabilities.FIREFOX
capabilities['proxy'] = {
'proxyType': 'MANUAL',
'httpProxy': f'{proxy.ip_address}:{proxy.port}',
'sslProxy': 'ip:port',
'socksUsername': proxy.login,
'socksPassword': proxy.password
}
browser = webdriver.Remote(command_executor='http://hub:4444/wd/hub',
desired_capabilities=capabilities,
browser_profile=profile)
selenium.common.exceptions.InvalidArgumentException: Message: Invalid
proxy configuration entry: socksPassword
tried socks proxy:
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'socksProxy': f'{proxy.ip_address}:{proxy.port}',
'socksUsername': proxy.login,
'socksPassword': proxy.password
})
browser = webdriver.Remote(command_executor='http://hub:4444/wd/hub',
desired_capabilities=capabilities,
browser_profile=profile,
proxy=proxy)
Message: Invalid proxy configuration entry: socksPassword
I also tried to set proxy via firefox profile, like here, but it seems not working because there is no option to set password for proxy.
Proxy is working because when i do request:
proxy = 'socks5://username:password#ip:port' resp = requests.get('https://api.ipify.org?format=json',
proxies=dict(http=proxy,
https=proxy)) print(resp.json())
returns correct result
The only solution is using proxy without password
Python3. You need to pip3 install geckodriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
# https://free-proxy-list.net/
proxy = '78.96.125.24:3128'
firefox_capabilities['proxy'] = {
"proxyType": "MANUAL",
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
}
browser = webdriver.Firefox(capabilities=firefox_capabilities)
browser.get('https://httpbin.org/ip')
# browser.get('https://www.google.pl')

Using a http proxy with headless firefox in Selenium webdriver in Python

I'm using Firefox headless like this:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
import os
import sys
# Set the MOZ_HEADLESS environment variable which casues Firefox to
# start in headless mode.
os.environ['MOZ_HEADLESS'] = '1'
# Select your Firefox binary.
binary = FirefoxBinary('/usr/bin/firefox', log_file=sys.stdout)
# Start selenium with the configured binary.
driver = webdriver.Firefox(firefox_binary=binary)
But now I want to add a http proxy that requires a user/password. After searching around, I tried the following:
from selenium.webdriver.common.proxy import Proxy, ProxyType
myProxy = "xx.xx.xx.xx:80"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
I also tried
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "xx.xx.xx.xx")
profile.set_preference("network.proxy.http_port", 80)
profile.update_preferences()
driver=webdriver.Firefox(firefox_binary=binary,firefox_profile=profile)
Finally, I tried adding "socksUsername" and "socksPassword" with the creds to the proxy, more out of desperation than any real hope.
Needless to say none of these works, and testing shows requests are still using my usual IP, not the proxy.
Also system-wide proxy is not an option in this case.
Where should the http proxy credentials live? How can I use a proxy with headless firefox?
Testing
driver.get("https://www.ipinfo.io");
driver.find_element_by_xpath('//h4/following-sibling::p').text
If your proxy requires a username and a password, you have to write it like that :
from selenium.webdriver.common.proxy import Proxy, ProxyType
myProxy = "username:password#proxyDomain:proxyPort"
proxy = Proxy({
'proxyType': ProxyType.MANUAL,
'httpProxy': myProxy,
'ftpProxy': myProxy,
'sslProxy': myProxy,
'noProxy': '' # set this value as desired
})
driver = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
Have you try setting up a profile manually with
./firefox --ProfileManager
manually setup the proxy and then load the profile you manually set
from selenium import webdriver
url = "https://mail.google.com"
fp = webdriver.FirefoxProfile('/Users/<username>/Library/Application Support/Firefox/Profiles/71v1uczn.default')
driver = webdriver.Firefox(fp)
You can try setting up an Environment variable "HTTP_PROXY" in following mnemonics:
http://<username>:<password>#<proxy_url>
Add your credentials separated by colon ':' before proxy url that is preceded by '#' for e.g.
http://username:password#proxy.com:8080/file.pac

Categories