How can I use proxy server using selenium and google chrome? I attached the code and I'm not sure if this will change the actual proxy server.
# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random
PROXY ="88.157.149.250:8080";
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# //a[starts-with(#href, 'https://www.amazon.com/')]/#href
LINKS_XPATH = '//*[contains(#id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
chrome_options=chrome_options)
browser.get(
'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
href = link.get_attribute('href')
print(href)
from selenium import webdriver
PROXY = "88.157.149.250:8080" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://google.com")
You can open the page https://www.whatismyip.com/my-ip-information/
chrome.get("https://www.whatismyip.com/my-ip-information/")
proxy = 192.168.22.1:8080
if proxy != None:
print('\nProxy ativado: ',proxy)
#chrome_options.add_argument('--proxy-server=%s' % proxy)
webdriver.DesiredCapabilities.CHROME['proxy'] = {
"httpProxy": proxy,
"ftpProxy": proxy,
"sslProxy": proxy,
"proxyType": "MANUAL",
}
webdriver.DesiredCapabilities.CHROME['acceptSslCerts']=True
print(webdriver.DesiredCapabilities.CHROME)
Related
How is it possible to change the proxy server in Selenium after starting the driver?
I saw several threads on this topic, but none of the answers were correct.
You can use not only Chrome but also Firefox.
If you have any ideas on how to change the proxy when the driver is open, please write.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
Path = ChromeDriverManager().install()
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
# options.add_argument('--no-zygote')
# options.add_argument('--single-process')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
proxy_file = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
proxy = ((random.choice(proxy_file)).replace("\n", ""))
options.add_argument('--proxy-server=%s' % proxy)
browser = webdriver.Chrome(Path, options=options)
browser.get('https://google.com')
# Here the proxy should change
browser.get('https://google.com')
No, you won't be able to change the proxy server using Selenium after starting the driver and the Browsing Context.
When you configure an instance of a ChromeDriver with ChromeOptions() to span a new Chrome Browsing Context the configuration gets baked within the chromedriver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through ChromeOptions() class to the WebDriver instance which is currently in execution.
Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.
A cleaner way would be to quit() the existing ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of proxy configuration as follows:
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=600,400')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-accelerated-2d-canvas')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
urls_to_visit = ['https://www.google.com/', 'https://stackoverflow.com/']
proxies = open("proxy.txt", "r", encoding="utf-8", errors="ignore").readlines()
for i in range(0, len(urls_to_visit)):
proxy = ((random.choice(proxies)).replace("\n", ""))
options.add_argument('--proxy-server=%s' % proxy)
browser = webdriver.Chrome(Path, options=options)
browser.get("{}".format(urls_to_visit[i]))
# perform the tasks
driver.quit()
References
You can find a couple of relevant discussions in:
How to rotate Selenium webrowser IP address
I am trying to make a Python script that goes to a site with a proxy, then refreshes, and goes to the other proxy in the proxys.txt file. ( this is for selenium, also a school project! )
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://example.com/")
You will have to be more specific, but if you want a way to use selenium with proxy, this is the way:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PROXY = "<IP>:<PORT>"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
"httpProxy": PROXY,
"ftpProxy": PROXY,
"sslProxy": PROXY,
"proxyType": "MANUAL",
}
with webdriver.Firefox() as driver:
driver.get("https://example.com/")
I am planning to set up an automated prosess on a website. But cant get through the message 'Checking your browser before accessing sit.no' when I try to reach the site using selenium. The message is from cloudflare.
I have been trying some code that i found here but it didnt work
1.
from selenium import webdriver
url = 'https://www.sit.no/'
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
driver.get(url)
2.
from selenium import webdriver
url = 'https://www.sit.no/'
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get(url)
3.
import undetected_chromedriver as uc
url = 'https://www.sit.no/'
driver= uc.Chrome()
driver.get(url)
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import subprocess
#other imports
subprocess.Popen(
'"C:\\Program Files\\yourpathtochrome\\chrome.exe" --remote-debugging-port=9222', shell=True)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get('https://www.sit.no/')
input()
first open the website manually and complete the browser check. now Close all chrome browsers and replace the path with your chrome.exe path. This will work ,
I try to connect http://username:password#ip:port proxy and options.add_argument(f'--proxy-server=%s' % proxy) not working. Can you help me?..
To connect a proxy through ChromeDriver you can use the following code block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
PROXY = "ip:port"
options = Options()
options.add_argument('--proxy-server=%s' % PROXY)
driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
am trying to use selenium to change my IP using a code I found
but am getting an error:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
`from selenium import webdriver
PROXY = "23.23.23.23:3128" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=http://%s' % PROXY)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://whatismyipaddress.com")`
Here, I hope it helps :)
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = "ip_addr:port"
prox.socks_proxy = "ip_addr:port"
prox.ssl_proxy = "ip_addr:port"
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
driver = webdriver.Chrome(desired_capabilities=capabilities)
I solved this error by adding the executable path to the chrome variable
Here is my code :
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=ipaddress:port')
driver = webdriver.Chrome(executable_path="your executable path",chrome_options=chrome_options)
driver.get('https://www.myip.com/')