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")
Related
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.
The following python code can get console log, but how can I access Security tab on Chrome devTools? E.g. I want to log about This page is secure or not (HTTPS) and the TLS certificate status.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
yoururl = "test_url"
caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'browser': 'ALL'}
# driver = webdriver.Chrome(desired_capabilities=caps)
driver = webdriver.Chrome('path_of_chromedriver', desired_capabilities=caps)
driver.get(yoururl)
time.sleep(5) # wait for all the data to arrive.
for log in driver.get_log('browser'):
print(log)
driver.close()
Ok, my solution is as follows:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import json
caps = DesiredCapabilities.CHROME.copy()
caps['goog:loggingPrefs'] = {
'performance': 'ALL',
}
caps['goog:perfLoggingPrefs'] = {
'enableNetwork': True
}
option = webdriver.ChromeOptions()
option.add_argument('--ignore-certificate-errors')
# option.add_experimental_option('w3c', False)
driver = webdriver.Chrome('chromedriver.exe', options=option, desired_capabilities=caps)
yoururl = "https://www.google.com.tw/"
driver.get(yoururl)
time.sleep(5)
for log_data in driver.get_log('performance'):
log_json = json.loads(log_data['message'])
log = log_json['message']
if log['method'] == 'Network.responseReceived' and log['params']['response']['url'] == yoururl:
print(f"securityState={log['params']['response']['securityState']}")
print(f"securityDetails={log['params']['response']['securityDetails']}")
driver.quit()
So yeah My problem is that selenium dont want to connect with an proxy to the site.Why? (here my code)
from fake_useragent import UserAgent
import undetected_chromedriver as uc
uc.install()
from time import sleep
from selenium import webdriver
from termcolor import colored
import random
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def gather_proxy():
proxies = []
with open('Source/proxies.txt', 'r', encoding='UTF-8') as file:
lines = file.readlines()
for line in lines:
proxies.append(line.replace('\n', ''))
return proxies
ua = UserAgent()
userAgent = ua.random
driver = webdriver.Chrome()
proxiess = random.choice(gather_proxy())
try:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument('--proxy-server=%s' % proxiess)
chrome_options.add_experimental_option('prefs', {
'credentials_enable_service': False,
'profile': {
'password_manager_enabled': False
}})
driver.get('https://www.wieistmeineip.de/')
sleep(30)
So yeah can you help me? And yeah I have to add some more information so yeah here we go I guess xD
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')
I have no idea why the following code returns the proxy as invalid only for the chrome browser help is appreciated. Below are the imports.
import requests
import json
import time
import random
import threading
from threading import Thread
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import datetime
from proxymanager import ProxyManager
from random import randint
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.options import Options
def getProxy():
try:
proxy_manager = ProxyManager('proxies.txt')
proxydict = proxy_manager.random_proxy()
proxies = proxydict.get_dict()
except:
proxies = []
return proxies
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=https://%s' %getProxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
I'll go out of a limb and guess the problem is with the proxy expansion - you're trying to pass a dict to Chrome instead of the actual proxy address. You want to get the actual value from the Proxy() class in your getProxy() function, e.g.:
def get_proxy(string_only=True):
try:
proxy_manager = ProxyManager("proxies.txt")
proxy = proxy_manager.random_proxy()
if string_only:
return proxy.proxy_string
return proxy.get_dict()
except (OSError, IOError, IndexError) as e: # couldn't load the file / file is empty
return None
# With Chrome:
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")
# with requests:
response = requests.get("http://whatismyipaddress.com", proxies=get_proxy(False))
# etc.
I'd also recommend to load the proxy list only once if you intend to call this function often and if the proxies.txt is a static file.
This works
def get_proxy():
try:
proxy_manager = ProxyManager("proxies.txt")
return proxy_manager.random_proxy().proxy_string
except (OSError, IOError) as e: # couldn't load the file
return None
chrome_options = webdriver.ChromeOptions()
proxy = get_proxy()
if proxy:
chrome_options.add_argument("--proxy-server=" + proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")