<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Selenium &#8211; Abdelhadi Dyouri</title>
	<atom:link href="https://adyouri.com/category/selenium/feed" rel="self" type="application/rss+xml" />
	<link>https://adyouri.com</link>
	<description>Learn Programming</description>
	<lastBuildDate>Wed, 20 Mar 2024 03:26:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>

<image>
	<url>https://adyouri.com/wp-content/uploads/2023/10/cropped-favicon-32x32.png</url>
	<title>Selenium &#8211; Abdelhadi Dyouri</title>
	<link>https://adyouri.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use a Proxy in Selenium v4 with Firefox &#038; Python (2024)</title>
		<link>https://adyouri.com/selenium-proxy-firefox</link>
		
		<dc:creator><![CDATA[Abdelhadi Dyouri]]></dc:creator>
		<pubDate>Mon, 18 Mar 2024 01:53:24 +0000</pubDate>
				<category><![CDATA[API Tutorials]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[proxy]]></category>
		<guid isPermaLink="false">https://adyouri.com/?p=1636</guid>

					<description><![CDATA[Have you been struggling to get a proxy to work in Selenium while using the Firefox web driver? Rest assured, in this tutorial, I will give you Python Selenium code that actually works for proxying requests in Selenium with a Firefox web driver. Selenium is a browser automation tool that is used to build bots,...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Have you been struggling to get a proxy to work in Selenium while using the Firefox web driver?</p>



<p class="wp-block-paragraph">Rest assured, in this tutorial, I will give you Python Selenium code that actually works for proxying requests in Selenium with a Firefox web driver.</p>



<p class="wp-block-paragraph">Selenium is a browser automation tool that is used to build bots, test web applications and scrape data from the internet. Its&nbsp;<a href="https://adyouri.com/python-web-scraping-libraries#4-selenium">Python library</a>&nbsp;is a popular choice for web automation and web scraping.</p>



<p class="wp-block-paragraph">If you’re not sure what’s a proxy, check out my&nbsp;<a href="https://adyouri.com/what-is-a-web-scraping-proxy">What is a Web Scraping Proxy?</a>&nbsp;guide.</p>



<p class="wp-block-paragraph">Without further ado, here is a clear, step-by-step guide on how to use a proxy in Selenium v4 with Firefox in Python.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/03/Firefox-Selenium-Proxy-1.png" alt="How to use a proxy in selenium v4 with Firefox" class="wp-image-1637" srcset="https://adyouri.com/wp-content/uploads/2024/03/Firefox-Selenium-Proxy-1.png 768w, https://adyouri.com/wp-content/uploads/2024/03/Firefox-Selenium-Proxy-1-300x200.png 300w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p class="wp-block-paragraph"><strong>Note: </strong>If you are using the Chrome webdriver, checkout my <a href="https://adyouri.com/selenium-proxy-python">How to Use a Proxy in Selenium v4 with Chrome</a> tutorial.</p>





<h2 class="wp-block-heading" id="how-to-use-a-proxy-in-selenium-with-firefox">How to Use a Proxy in Selenium v4 with Firefox</h2>



<p class="wp-block-paragraph">For experienced developers with no time for explanations, here is the Python code you need to use a Proxy in Selenium with the Firefox driver:</p>



<pre class="wp-block-code"><code>import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By

options = Options()
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "207.2.120.19")
options.set_preference("network.proxy.http_port", 80)

driver = webdriver.Firefox(
    options=options
)

driver.get("http://httpbin.org/ip")

print(driver.find_element(By.TAG_NAME, "body").text)

time.sleep(5)
driver.close()
</code></pre>



<p class="wp-block-paragraph">The output should have your proxy server’s IP address instead of your local machine’s IP:</p>



<figure class="wp-block-image size-full"><img decoding="async" width="708" height="454" src="https://adyouri.com/wp-content/uploads/2024/03/carbon-1.png" alt="selenium proxy in firefox" class="wp-image-1595" srcset="https://adyouri.com/wp-content/uploads/2024/03/carbon-1.png 708w, https://adyouri.com/wp-content/uploads/2024/03/carbon-1-300x192.png 300w" sizes="(max-width: 708px) 100vw, 708px" /></figure>



<p class="wp-block-paragraph">That’s it! You are now using a proxy in Selenium with a Firefox web driver. If the code above is unclear, or you can’t get it to work, then carefully read the full detailed guide below.</p>



<h2 class="wp-block-heading" id="step-01-–-installing-selenium">Step 01 – Installing Selenium</h2>



<p class="wp-block-paragraph">First, make sure you have Firefox installed on your system.</p>



<p class="wp-block-paragraph">Next, create a new Python folder called&nbsp;<code>selenium-proxy</code>:</p>



<pre class="wp-block-code"><code>mkdir selenium-proxy</code></pre>



<p class="wp-block-paragraph">Create a new Python virtual environment inside it:</p>



<pre class="wp-block-code"><code>python -m venv env</code></pre>



<p class="wp-block-paragraph">Activate the virtual environment:</p>



<pre class="wp-block-code"><code>source env/bin/activate</code></pre>



<p class="wp-block-paragraph">Install Selenium:</p>



<pre class="wp-block-code"><code>pip install selenium</code></pre>



<p class="wp-block-paragraph">Next, add a file called&nbsp;<code>scraper.py</code>&nbsp;and then add the following code to it:</p>



<pre class="wp-block-code"><code>from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.python.org")

print(driver.title)

print(driver.current_url)

driver.close()</code></pre>



<p class="wp-block-paragraph">Here’s a brief explanation of each part:</p>



<ol class="wp-block-list">
<li><code>from selenium import webdriver</code>: This line imports the Selenium module which provides tools for automating web browsers.</li>



<li><code>driver = webdriver.Firefox()</code>: This line creates an instance of the Firefox web driver, which allows Python to control the Firefox web browser.</li>



<li><code>driver.get("https://www.python.org")</code>: This instructs the web driver to navigate to the URL “<a href="https://www.python.org/" target="_blank" rel="noopener">https://www.python.org</a>”.</li>



<li><code>print(driver.title)</code>: This prints the title of the web page currently loaded in the browser (in this case, the Python website).</li>



<li><code>print(driver.current_url)</code>: This prints the current URL of the web page loaded in the browser.</li>



<li><code>driver.close()</code>: This closes the browser window opened by the web driver.</li>
</ol>



<p class="wp-block-paragraph">Now run&nbsp;<code>scraper.py</code>:</p>



<pre class="wp-block-code"><code>python scraper.py</code></pre>



<h3 class="wp-block-heading" id="output">Output</h3>



<pre class="wp-block-code"><code>Welcome to Python.org
https:&#47;&#47;www.python.org/</code></pre>



<p class="wp-block-paragraph">The preceding output indicates your Selenium Firefox environment is working properly. If you are facing issues, check that you’ve correctly set up your environment.</p>



<h2 class="wp-block-heading" id="step-02-–-using-a-proxy-with-selenium-in-firefox">Step 02 – Using a Proxy with Selenium in Firefox</h2>



<p class="wp-block-paragraph">With Selenium and Firefox successfully set up. Let’s add a proxy.</p>



<p class="wp-block-paragraph">Inside your&nbsp;<code>selenium-proxy</code>&nbsp;project folder, create a new file called&nbsp;<code>proxy.py</code>, and add the following code to it:</p>



<pre class="wp-block-code"><code>import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By

options = Options()
options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.http", "207.2.120.19")
options.set_preference("network.proxy.http_port", 80)

driver = webdriver.Firefox(
    options=options
)

driver.get("http://httpbin.org/ip")

print(driver.find_element(By.TAG_NAME, "body").text)

time.sleep(5)
driver.close()</code></pre>



<p class="wp-block-paragraph">Here is a breakdown of the preceding code:</p>



<ol class="wp-block-list">
<li><code>import time</code>: This imports the time module, which is used to add time delays in the script.</li>



<li><code>from selenium import webdriver</code>: This imports the webdriver module from the Selenium library, which provides tools for automating web browsers.</li>



<li><code>from selenium.webdriver.firefox.options import Options</code>: This imports the Options class from the Selenium Firefox module, which allows customizing browser options.</li>



<li><code>from selenium.webdriver.common.by import By</code>: This imports the By class from the Selenium library, which provides mechanisms for selecting elements in the web page.</li>



<li><code>options = Options()</code>: This creates an instance of the Options class, which is used to set browser preferences.</li>



<li><code>options.set_preference("network.proxy.type", 1)</code>: This sets the browser’s network proxy type to use a manual proxy configuration.</li>



<li><code>options.set_preference("network.proxy.http", "207.2.120.19")</code>: This sets the HTTP proxy address to “207.2.120.19”.</li>



<li><code>options.set_preference("network.proxy.http_port", 80)</code>: This sets the HTTP proxy port to 80.</li>



<li><code>driver = webdriver.Firefox(options=options)</code>: This initializes a new instance of the Firefox web driver with the custom options set.</li>



<li><code>driver.get("http://httpbin.org/ip")</code>: This instructs the browser to navigate to the URL “<a href="http://httpbin.org/ip" target="_blank" rel="noopener">http://httpbin.org/ip</a>”, which is a website that returns information about the client’s IP address.</li>



<li><code>print(driver.find_element(By.TAG_NAME, "body").text)</code>: This finds the body element of the web page and prints its text content, which in this case should contain information about the client’s IP address.</li>



<li><code>time.sleep(5)</code>: This pauses the script execution for 5 seconds, allowing time for the web page to load and display its content.</li>



<li><code>driver.close()</code>: This closes the browser window opened by the web driver.</li>
</ol>



<p class="wp-block-paragraph">The output should be as follows:</p>



<pre class="wp-block-code"><code>{
  "origin": "207.2.120.19"
}</code></pre>



<p class="wp-block-paragraph">To use an SSL Proxy with Selenium in Firefox, implement the following options:</p>



<pre class="wp-block-code"><code>options.set_preference("network.proxy.type", 1)
options.set_preference("network.proxy.ssl", SSL_IP)
options.set_preference("network.proxy.ssl_port", SSL_PORT)</code></pre>



<p class="wp-block-paragraph">To use proxy authentication in Firefox, check out the next step.</p>



<h2 class="wp-block-heading" id="step-03-–-selenium-proxy-authentication-with-python-in-firefox">Step 03 – Selenium Proxy Authentication with Python in Firefox</h2>



<figure class="wp-block-image size-full"><img decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1.png" alt="How to use Proxy Authentication in Selenium v4 with Firefox" class="wp-image-1598" srcset="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1.png 768w, https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1-300x200.png 300w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<p class="wp-block-paragraph">If you want to use a proxy that requires authentication with a username and password. The simplest way to achieve this is by using the&nbsp;<a href="https://pypi.org/project/selenium-wire/" target="_blank" rel="noopener"><code>selenium-wire</code></a>&nbsp;extension. Selenium Wire makes it really easy to use both unauthenticated and authenticated proxies in Selenium.</p>



<p class="wp-block-paragraph">To install Selenium Wire:</p>



<pre class="wp-block-code"><code>pip install selenium-wire</code></pre>



<p class="wp-block-paragraph">To use proxy authentication with Selenium Wire in Firefox, you can just pass the username and password of your proxy server in the&nbsp;<code>options</code>&nbsp;dictionary like so:</p>



<pre class="wp-block-code"><code>import time
from seleniumwire import webdriver

options = {
'proxy': {
    'http': 'http://USERNAME:PASSWORD@PROXY_SERVER:PORT',
    'https': 'https://USERNAME:PASSWORD@PROXY_SERVER:PORT',
    'no_proxy': 'localhost,127.0.0.1,dev_server:8080'
    }
}

driver = webdriver.Firefox(seleniumwire_options=options)

driver.get('http://httpbin.org/ip')
time.sleep(5)
driver.close()</code></pre>



<p class="wp-block-paragraph">If you’re proxy uses the&nbsp;<code>Bearer</code>&nbsp;scheme, you can pass your authorization token in a&nbsp;<code>Proxy-Authorization</code>&nbsp;header using the&nbsp;<code>custom_authorization</code>&nbsp;option:</p>



<pre class="wp-block-code"><code>options = {
    'proxy': {
        'https': 'https://192.168.10.100:8888',  # No username or password used
        'custom_authorization': 'Bearer mytoken123'  # Custom Proxy-Authorization header value
    }
}</code></pre>



<p class="wp-block-paragraph">For more on how to use proxies, check out the&nbsp;<a href="https://pypi.org/project/selenium-wire/#proxies" target="_blank" rel="noopener">Selenium Wire Documentation</a></p>



<h4 class="wp-block-heading" id="sources">Sources</h4>



<ul class="wp-block-list">
<li><a href="https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.proxy" target="_blank" rel="noopener">Python Selenium Documentation</a></li>



<li><a href="https://pypi.org/project/selenium-wire/#proxies" target="_blank" rel="noopener">Selenium Wire Documentation</a></li>
</ul>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<p class="wp-block-paragraph">Congrats, you now know how to use a proxy in Python Selenium v4.0 with a Firefox web driver. You also learned how to use proxy authentication in Selenium for proxy servers protected with a username and password.</p>



<h2 class="wp-block-heading" id="faq-selenium-firefox-and-proxies">FAQ: Selenium, Firefox, and Proxies</h2>



<h3 class="wp-block-heading" id="why-is-using-proxies-important-in-web-scraping-with-selenium-and-firefox">Why is using proxies important in web scraping with Selenium and Firefox?</h3>



<p class="wp-block-paragraph">Proxies are essential in web scraping to mask your real IP address and avoid being blocked or rate-limited by websites.</p>



<p class="wp-block-paragraph">By rotating through a pool of proxies, you can distribute requests across multiple IP addresses, mitigating the risk of detection and ensuring uninterrupted scraping operations.</p>



<h3 class="wp-block-heading" id="can-i-rotate-proxies-automatically-while-scraping-with-selenium-and-firefox">Can I rotate proxies automatically while scraping with Selenium and Firefox?</h3>



<p class="wp-block-paragraph">Yes, you can rotate proxies automatically by managing a pool of proxies and switching between them in your Selenium script.</p>



<p class="wp-block-paragraph">Implementing a proxy rotation mechanism involves periodically selecting a new proxy from the pool and updating the Firefox options accordingly. This approach helps distribute requests evenly and reduces the likelihood of IP bans or detection.</p>



<h3 class="wp-block-heading" id="are-there-any-limitations-or-considerations-when-using-proxies-with-selenium-and-firefox-for-web-scraping">Are there any limitations or considerations when using proxies with Selenium and Firefox for web scraping?</h3>



<p class="wp-block-paragraph">While proxies provide anonymity and flexibility in web scraping, there are some considerations to keep in mind.</p>



<p class="wp-block-paragraph">Firstly, free proxies may be unreliable or slow, affecting scraping performance. Additionally, websites may detect and block proxy IP addresses, requiring frequent updates to your proxy pool. Also, using proxies may incur additional costs, especially when utilizing premium or rotating proxy services.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Use a Proxy in Selenium v4 &#8211; Python (Ultimate Guide)</title>
		<link>https://adyouri.com/selenium-proxy-python</link>
		
		<dc:creator><![CDATA[Abdelhadi Dyouri]]></dc:creator>
		<pubDate>Sun, 10 Mar 2024 23:23:10 +0000</pubDate>
				<category><![CDATA[Selenium]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[web scraping]]></category>
		<guid isPermaLink="false">https://adyouri.com/?p=1589</guid>

					<description><![CDATA[Trying to use a proxy in Selenium v4? You’re in the right place! I’ve spent hours figuring out how to use a proxy in Selenium with a Chrome driver for one of my web scraping projects. Google search results were full of outdated tutorials with code that doesn’t work. So, I went into the depths...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Trying to use a proxy in Selenium v4? You’re in the right place!</p>



<p class="wp-block-paragraph">I’ve spent hours figuring out how to use a proxy in Selenium with a Chrome driver for one of my web scraping projects.</p>



<p class="wp-block-paragraph">Google search results were full of outdated tutorials with code that doesn’t work. So, I went into the depths of the latest <a href="https://www.selenium.dev/documentation/" target="_blank" rel="noopener">Selenium documentation</a>, and I finally figured it out. Now, I present to you a Selenium proxy tutorial with Python code that actually works.</p>



<h4 class="wp-block-heading"><strong>Notes</strong></h4>



<ul class="wp-block-list">
<li>I am using the Chrome driver in this tutorial.</li>
</ul>



<ul class="wp-block-list">
<li>If you’re not sure what’s a proxy, check out my <a href="https://adyouri.com/what-is-a-web-scraping-proxy">What is a Web Scraping Proxy?</a> guide.</li>
</ul>



<p class="wp-block-paragraph">Selenium is a browser automation tool that is used to build bots, test web applications and <a href="https://adyouri.com/web-scraping-use-cases">scrape data</a> from the internet. Its <a href="https://adyouri.com/python-web-scraping-libraries#4-selenium">Python library</a> is a popular choice for web automation and web scraping.</p>



<p class="wp-block-paragraph"></p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-1.png" alt="proxy in selenium " class="wp-image-1590" style="width:771px;height:auto" srcset="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-1.png 768w, https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-1-300x200.png 300w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>



<p class="wp-block-paragraph"><strong>Note:&nbsp;</strong>If you are using the Firefox Webdriver, checkout my <a href="https://adyouri.com/selenium-proxy-firefox">How to Use a Proxy in Selenium v4 with Firefox &amp; Python</a>&nbsp;tutorial.</p>





<h2 class="wp-block-heading" id="how-to-use-a-proxy-with-selenium">How to Use a Proxy in Selenium</h2>



<p class="wp-block-paragraph">For experienced developers, here is a quick and short answer for how to use a Proxy in Selenium with the chrome driver:</p>



<pre class="wp-block-code"><code>import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

PROXY = "http://207.2.120.19:80"

options = Options()
options.add_argument(f'--proxy-server={PROXY}')


driver = webdriver.Chrome(
    options=options
)

driver.get("http://httpbin.org/ip")

print(driver.find_element(By.TAG_NAME, "body").text)

time.sleep(5)
driver.close()</code></pre>



<p class="wp-block-paragraph">The output should be as follows:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="708" height="454" src="https://adyouri.com/wp-content/uploads/2024/03/carbon-1.png" alt="selenium proxy python output" class="wp-image-1595" srcset="https://adyouri.com/wp-content/uploads/2024/03/carbon-1.png 708w, https://adyouri.com/wp-content/uploads/2024/03/carbon-1-300x192.png 300w" sizes="auto, (max-width: 708px) 100vw, 708px" /></figure>



<p class="wp-block-paragraph">That’s it, you are now using a proxy with selenium. If the preceding code is unclear, or you can’t get it to work, then carefully read the full detailed guide below.</p>



<h2 class="wp-block-heading" id="getting-selenium-to-work">Step 01 &#8211; Getting Selenium to Work</h2>



<p class="wp-block-paragraph">Previously, you had to download a&nbsp;<code>chromedriver</code>&nbsp;web driver for Selenium to work properly. With Selenium version 4, you no longer need to install a web driver.</p>



<p class="wp-block-paragraph">However, if Selenium is not working for you, you may still need to download the&nbsp;<code>chromedriver</code>. To do so, follow these steps:</p>



<ul class="wp-block-list">
<li>Go to Chrome’s&nbsp;<a href="https://sites.google.com/chromium.org/driver/downloads" target="_blank" rel="noopener">Downloads Page</a></li>



<li>Select your Chrome’s version.</li>



<li>If you are using Chrome version 115 or newer, please consult the&nbsp;<a href="https://googlechromelabs.github.io/chrome-for-testing/" target="_blank" rel="noopener">Chrome for Testing availability dashboard</a>.</li>



<li>Select the&nbsp;<code>chromedriver</code>&nbsp;that’s suitable for your OS from the table, download it, then add it to your&nbsp;<code>PATH</code>.</li>
</ul>



<p class="wp-block-paragraph">Make sure to run the&nbsp;<code>chromedriver</code>&nbsp;and your Chrome or Chromium browser at least once before proceeding. This step may not be necessary for some users:</p>



<pre class="wp-block-code"><code>$ chromedriver
</code></pre>



<p class="wp-block-paragraph">Now, create a new Python folder called&nbsp;<code>selenium-proxy</code>:</p>



<pre class="wp-block-code"><code>mkdir selenium-proxy
</code></pre>



<p class="wp-block-paragraph">Create a new Python virtual environment inside it:</p>



<pre class="wp-block-code"><code>python -m venv env
</code></pre>



<p class="wp-block-paragraph">Activate the virtual environment:</p>



<pre class="wp-block-code"><code>source env/bin/activate
</code></pre>



<p class="wp-block-paragraph">Install selenium:</p>



<pre class="wp-block-code"><code>pip install selenium
</code></pre>



<p class="wp-block-paragraph">Next, add a file called&nbsp;<code>scraper.py</code>&nbsp;and then add the following code to it:</p>



<pre class="wp-block-code"><code># scraper.py
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.python.org")

print(driver.title)

print(driver.current_url)

time.sleep(5)
driver.close()
</code></pre>



<p class="wp-block-paragraph">The preceding script opens the Python website using a Chrome WebDriver, prints the title and URL of the page, pauses for 5 seconds, and then closes the browser.</p>



<p class="wp-block-paragraph">Here is a more detailed overview:</p>



<ol class="wp-block-list">
<li><strong>Import Libraries:</strong>
<ul class="wp-block-list">
<li><code>time</code>: Used for adding delays in the script.</li>



<li><code>webdriver</code>&nbsp;from the&nbsp;<code>selenium</code>&nbsp;library: Provides a common interface for interacting with web browsers. In this case, it’s using the Chrome WebDriver.</li>
</ul>
</li>



<li><strong>Instantiate WebDriver:</strong>
<ul class="wp-block-list">
<li><code>driver = webdriver.Chrome()</code>: This line creates an instance of the Chrome WebDriver, allowing the script to control and automate the Google Chrome browser.</li>
</ul>
</li>



<li><strong>Navigate to a URL:</strong>
<ul class="wp-block-list">
<li><code>driver.get("https://www.python.org")</code>: Instructs the WebDriver to open the specified URL, in this case, the official Python website.</li>
</ul>
</li>



<li><strong>Print Page Title and URL:</strong>
<ul class="wp-block-list">
<li><code>print(driver.title)</code>: Outputs the title of the current web page opened by the WebDriver (in this case, the Python website).</li>



<li><code>print(driver.current_url)</code>: Outputs the URL of the current web page.</li>
</ul>
</li>



<li><strong>Pause Execution (Sleep):</strong>
<ul class="wp-block-list">
<li><code>time.sleep(5)</code>: Introduces a 5-second delay in the script’s execution. This allows you to see the website loaded on chrome, instead of being closed right away.</li>
</ul>
</li>



<li><strong>Close the Browser:</strong>
<ul class="wp-block-list">
<li><code>driver.close()</code>: Closes the browser window that the WebDriver is controlling.</li>
</ul>
</li>
</ol>



<p class="wp-block-paragraph">Now, run&nbsp;<code>scraper.py</code>:</p>



<pre class="wp-block-code"><code>python scraper.py
</code></pre>



<p class="wp-block-paragraph">You should see the following output:</p>



<pre class="wp-block-code"><code>Welcome to Python.org
https:&#47;&#47;www.python.org/
</code></pre>



<p class="wp-block-paragraph">This means Selenium and&nbsp;<code>chromedriver</code>&nbsp;are working properly.</p>



<h2 class="wp-block-heading" id="using-a-proxy-with-selenium">Step 02 &#8211; Using a Proxy with Selenium</h2>



<p class="wp-block-paragraph">Now, let’s move on to how to use a proxy in your python selenium code.</p>



<p class="wp-block-paragraph">Inside your&nbsp;<code>selenium-proxy</code>&nbsp;project folder, create a new file called&nbsp;<code>proxy.py</code>, and add the following code to it:</p>



<pre class="wp-block-code"><code>import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

PROXY = "http://207.2.120.19:80"

options = Options()
options.add_argument(f'--proxy-server={PROXY}')


driver = webdriver.Chrome(
    options=options
)

driver.get("http://httpbin.org/ip")

print(driver.find_element(By.TAG_NAME, "body").text)

time.sleep(5)
driver.close()
</code></pre>



<p class="wp-block-paragraph">Let’s break down the code step by step:</p>



<h4 class="wp-block-heading" id="import-libraries">1. Import Libraries:</h4>



<pre class="wp-block-code"><code>import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
</code></pre>



<p class="wp-block-paragraph">Here are the imports:</p>



<ul class="wp-block-list">
<li><code>time</code>: Provides time-related functions for introducing delays.</li>



<li><code>webdriver</code>: From the Selenium library, used for automating web browser interactions.</li>



<li><code>Options</code>: A class from&nbsp;<code>selenium.webdriver.chrome.options</code>&nbsp;to configure Chrome WebDriver options.</li>



<li><code>By</code>: A class from&nbsp;<code>selenium.webdriver.common.by</code>&nbsp;to specify the mechanism used to locate elements on a webpage.</li>
</ul>



<h4 class="wp-block-heading" id="define-proxy-information">2. Define Proxy Information:</h4>



<pre class="wp-block-code"><code>PROXY = "http://207.2.120.19:80"
</code></pre>



<p class="wp-block-paragraph">Sets a proxy server address (<a href="http://207.2.120.19/"><code>http://207.2.120.19:80</code></a>). This is used to route the web traffic through a specified proxy server.</p>



<h4 class="wp-block-heading" id="configure-chrome-webdriver-with-proxy">3. Configure Chrome WebDriver with Proxy:</h4>



<pre class="wp-block-code"><code>options = Options()
options.add_argument(f'--proxy-server={PROXY}')
</code></pre>



<ul class="wp-block-list">
<li>Creates an instance of&nbsp;<code>Options</code>&nbsp;class to configure the Chrome WebDriver.</li>



<li>Adds a command-line argument to specify the proxy server for the Chrome WebDriver.</li>
</ul>



<h4 class="wp-block-heading" id="initialize-webdriver">4. Initialize WebDriver:</h4>



<pre class="wp-block-code"><code>driver = webdriver.Chrome(options=options)
</code></pre>



<p class="wp-block-paragraph">Creates a Chrome WebDriver instance with the configured options.</p>



<h4 class="wp-block-heading" id="navigate-to-a-url">5. Navigate to a URL:</h4>



<pre class="wp-block-code"><code>driver.get("http://httpbin.org/ip")
</code></pre>



<p class="wp-block-paragraph">Directs the WebDriver to open the specified URL (<a href="http://httpbin.org/ip" target="_blank" rel="noopener"><code>http://httpbin.org/ip</code></a>).</p>



<h4 class="wp-block-heading" id="find-and-print-page-content">6. Find and Print Page Content:</h4>



<pre class="wp-block-code"><code>print(driver.find_element(By.TAG_NAME, "body").text)
</code></pre>



<p class="wp-block-paragraph">Finds the&nbsp;<code>&lt;body&gt;</code>&nbsp;element of the webpage and prints its text content.&nbsp;<code><strong><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-theme-palette-1-color">By.TAG_NAME</mark></strong></code>&nbsp;is used to locate the element by its HTML tag name.</p>



<h4 class="wp-block-heading" id="pause-execution-sleep">7. Pause Execution (Sleep):</h4>



<pre class="wp-block-code"><code>time.sleep(5)
</code></pre>



<p class="wp-block-paragraph">Introduces a 5-second delay so that the Chrome browser doesn’t close right away.</p>



<h4 class="wp-block-heading" id="close-the-browser">8. Close the Browser:</h4>



<pre class="wp-block-code"><code>driver.close()
</code></pre>



<p class="wp-block-paragraph">Closes the browser window controlled by the WebDriver.</p>



<h3 class="wp-block-heading" id="output">Output</h3>



<p class="wp-block-paragraph">The output should have your proxy’s IP address instead of your local machine’s IP address:</p>



<pre class="wp-block-code"><code>{
  "origin": "207.2.120.19"
}
</code></pre>



<h2 class="wp-block-heading" id="using-an-proxy-authentication-with-selenium">Step 03 &#8211; Selenium Proxy Authentication with Python in Chrome</h2>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="768" height="512" src="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1.png" alt="Selenium Proxy Authentication" class="wp-image-1598" srcset="https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1.png 768w, https://adyouri.com/wp-content/uploads/2024/03/Selenium-Proxy-Auth-1-300x200.png 300w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>



<p class="wp-block-paragraph">If you want to use a proxy that requires authentication with a username and password. The simplest way to achieve this is by using the&nbsp;<a href="https://pypi.org/project/selenium-wire/" target="_blank" rel="noopener"><code>selenium-wire</code></a>&nbsp;extension.</p>



<p class="wp-block-paragraph">Selenium Wire extends Selenium’s Python bindings to give you access to the underlying requests made by the browser. You code in the same way as you do with Selenium, but you get extra APIs for inspecting requests and responses. Selenium Wire also makes it really easy to use both unauthenticated and authenticated proxies in Selenium.</p>



<p class="wp-block-paragraph">To install Selenium Wire:</p>



<pre class="wp-block-code"><code>pip install selenium-wire</code></pre>



<p class="wp-block-paragraph">Here is how to use an open unauthenticated proxy with Selenium Wire:</p>



<pre class="wp-block-code"><code>import time
from seleniumwire import webdriver

PROXY = "45.77.194.89:38869"

options = {
    'proxy': {
        'http': f'http://{PROXY}',
        'https': f'https://{PROXY}',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

driver = webdriver.Chrome(seleniumwire_options=options)

driver.get('http://httpbin.org/ip')
time.sleep(5)
driver.close()
</code></pre>



<p class="wp-block-paragraph">As you can see, you can just specify the IP address and port, pass the options to a&nbsp;<code>seleniumwire_options</code>&nbsp;parameter, and that’s it!</p>



<p class="wp-block-paragraph">To use <strong>proxy authentication</strong> with Selenium Wire, you can just pass the&nbsp;<code>username</code>&nbsp;and&nbsp;<code>password</code>&nbsp;of your proxy server in the&nbsp;<code>options</code>&nbsp;dictionary like so:</p>



<pre class="wp-block-code"><code>import time
from seleniumwire import webdriver

options = {
    'proxy': {
        'http': 'http://USERNAME:PASSWORD@PROXY_SERVER:PORT',
        'https': 'https://USERNAME:PASSWORD@PROXY_SERVER:PORT',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

driver = webdriver.Chrome(seleniumwire_options=options)

driver.get('http://httpbin.org/ip')
time.sleep(5)
driver.close()
</code></pre>



<p class="wp-block-paragraph">If your proxy uses the&nbsp;<code>Bearer</code>&nbsp;scheme, you can pass your authorization token in a&nbsp;<code>Proxy-Authorization</code>&nbsp;header using the&nbsp;<code>custom_authorization</code>&nbsp;option:</p>



<pre class="wp-block-code"><code>options = {
    'proxy': {
        'https': 'https://192.168.10.100:8888',  # No username or password used
        'custom_authorization': 'Bearer mytoken123'  # Custom Proxy-Authorization header value
    }
}
</code></pre>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<p class="wp-block-paragraph">Congrats, you now know how to use a proxy in Python Selenium v4.0 with a Chrome web driver. You also learned how to use proxy authentication in Selenium for proxy servers protected with a username and password.</p>



<h2 class="wp-block-heading">Sources</h2>



<ul class="wp-block-list">
<li><a href="https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.proxy" target="_blank" rel="noopener">Python Selenium Documentation</a></li>



<li><a href="https://pypi.org/project/selenium-wire/#proxies" target="_blank" rel="noopener">Selenium Wire Documentation</a></li>
</ul>



<h2 class="wp-block-heading" id="faqs">FAQs</h2>



<h3 class="wp-block-heading" id="why-use-a-proxy-with-selenium">Why use a proxy with Selenium?</h3>



<p class="wp-block-paragraph">Using a proxy with Selenium offers several benefits:</p>



<ol class="wp-block-list">
<li><strong>IP Anonymity:</strong>&nbsp;Proxies allow you to conceal your IP address, providing anonymity during web scraping or automated testing.</li>



<li><strong>Geographic Targeting:</strong>&nbsp;Proxies enable you to access websites as if you were located in a different geographic region, useful for testing or gathering region-specific data.</li>



<li><strong>Bypass Restrictions:</strong>&nbsp;Proxies help bypass IP-based restrictions, allowing access to websites that may block requests from specific IPs.</li>



<li><strong>Load Management:</strong>&nbsp;Distributing requests through different IP addresses using proxies can help manage server loads and reduce the risk of being flagged for excessive traffic.</li>



<li><strong>Content Verification:</strong>&nbsp;Proxies aid in verifying content availability and consistency across different regions or networks, ensuring a more comprehensive testing approach.</li>
</ol>



<h3 class="wp-block-heading" id="can-using-a-proxy-with-selenium-help-prevent-ip-blocking-during-web-scraping">Can using a proxy with Selenium help prevent IP blocking during web scraping?</h3>



<p class="wp-block-paragraph">Yes, using a proxy with Selenium can help prevent IP blocking during web scraping.</p>



<p class="wp-block-paragraph">By rotating IP addresses through proxies, you distribute requests, making it harder for websites to detect and block automated traffic.</p>



<p class="wp-block-paragraph">This enhances the resilience of your web scraping activities and reduces the risk of being flagged as a bot.</p>



<h3 class="wp-block-heading" id="are-there-free-proxies-available-for-use-with-selenium-or-do-i-need-to-purchase-them">Are there free proxies available for use with Selenium, or do I need to purchase them?</h3>



<p class="wp-block-paragraph">Both free and paid proxies are available. Free proxies may have limitations in terms of reliability, speed, and security.</p>



<p class="wp-block-paragraph">For more demanding tasks or projects, it’s often advisable to consider paid proxy services that offer better performance, dedicated support, and enhanced features.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: adyouri.com @ 2026-10-13 23:21:44 by W3 Total Cache
-->