Skip to content

Commit cb439e6

Browse files
author
oxyjonas
authored
Add files via upload
1 parent 32969fd commit cb439e6

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
# selenium-proxy-integration
1+
# Oxylabs’ Residential Proxies integration with Selenium
2+
3+
[<img src="https://img.shields.io/static/v1?label=&message=Python&color=brightgreen" />](https://github.com/topics/python) [<img src="https://img.shields.io/static/v1?label=&message=Selenium&color=orange" />](https://github.com/topics/selenium) [<img src="https://img.shields.io/static/v1?label=&message=Web-Scraping&color=yellow" />](https://github.com/topics/web-scraping) [<img src="https://img.shields.io/static/v1?label=&message=Rotating%20Proxies&color=blueviolet" />](https://github.com/topics/rotating-proxies)
4+
5+
## Requirements
6+
For the integration to work, you'll need to install Selenium on your system. You can do it using `pip` command:
7+
```bash
8+
pip install selenium
9+
```
10+
Another required package is `webdriver-manager`. It's a package that simplifies the management of binary drivers for different browsers, so you don't need to manually download a new version of a web driver after each update. Visit the [official project directory](https://pypi.org/project/webdriver-manager/) on pypi to find out more information. You can install the following using `pip` as well.
11+
```bash
12+
pip install webdriver-manager
13+
```
14+
Required version of Python: `Python 3.5` (or higher)
15+
## Proxy Authentication
16+
For proxies to work, you'll need to specify your account credentials inside the [main.py](https://github.com/oxylabs/selenium-proxy-integration/blob/main/main.py) file.
17+
```python
18+
USERNAME = "your_username"
19+
PASSWORD = "your_password"
20+
HOST = "pr.oxylabs.io"
21+
PORT = 7777
22+
```
23+
Adjust the `your_username` and `your_password` fields with the username and password of your Oxylabs account.
24+
## Country-Specific Entry Node
25+
If you want, you can also specify the entry node of a specific country:
26+
```python
27+
COUNTRY = "US"
28+
```
29+
To do that, adjust the `country` variable to any country that Oxylabs support. You can check out our [documentation](https://developers.oxylabs.io/residential-proxies/#country-specific-entry-nodes) for a complete list of country-specific entry nodes.
30+
31+
## Testing Proxy Connection
32+
You can test proxy connection by visiting https://ip.oxylabs.io/. It will return your current IP address.
33+
```python
34+
try:
35+
driver.get("https://ip.oxylabs.io/")
36+
time.sleep(5)
37+
finally:
38+
driver.close()
39+
```
40+
41+
## Full Code
42+
```python
43+
44+
import time
45+
from selenium import webdriver
46+
from webdriver_manager.chrome import ChromeDriverManager
47+
from proxies import chrome_proxy
48+
49+
USERNAME = "your_username"
50+
PASSWORD = "your_password"
51+
HOST = "pr.oxylabs.io"
52+
PORT = 7777
53+
COUNTRY = "US"
54+
55+
options = webdriver.ChromeOptions()
56+
proxy_ext = chrome_proxy(USERNAME, PASSWORD, HOST, PORT, COUNTRY)
57+
options.add_extension(proxy_ext)
58+
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
59+
60+
try:
61+
driver.get("https://ip.oxylabs.io/")
62+
time.sleep(5)
63+
finally:
64+
driver.close()
65+
```
66+
If you're having any trouble integrating proxies with Selenium and this guide didn't help you - feel free to contact Oxylabs customer support at support@oxylabs.io.
67+
68+

main.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import time
2+
from selenium import webdriver
3+
# A package to have a chromedriver always up-to-date.
4+
from webdriver_manager.chrome import ChromeDriverManager
5+
from proxies import chrome_proxy
6+
7+
USERNAME = "your_username"
8+
PASSWORD = "your_password"
9+
HOST = "pr.oxylabs.io"
10+
PORT = 7777
11+
# Specify country code if you want proxies from a single country, e.g. `US`.
12+
# Otherwise - set the variable to `None`.
13+
COUNTRY = "US"
14+
15+
options = webdriver.ChromeOptions()
16+
proxy_ext = chrome_proxy(USERNAME, PASSWORD, HOST, PORT, COUNTRY)
17+
options.add_extension(proxy_ext)
18+
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
19+
20+
try:
21+
driver.get("https://ip.oxylabs.io/")
22+
time.sleep(5)
23+
finally:
24+
driver.close()

proxies.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import zipfile
2+
from typing import Optional
3+
4+
MANIFEST_JSON = """
5+
{
6+
"version": "1.0.0",
7+
"manifest_version": 2,
8+
"name": "Oxylabs Selenium Proxy Integration",
9+
"permissions": [
10+
"proxy",
11+
"tabs",
12+
"unlimitedStorage",
13+
"storage",
14+
"<all_urls>",
15+
"webRequest",
16+
"webRequestBlocking"
17+
],
18+
"background": {
19+
"scripts": ["background.js"]
20+
},
21+
"minimum_chrome_version":"22.0.0"
22+
}
23+
"""
24+
25+
BACKGROUND_JS = """
26+
var config = {
27+
mode: "fixed_servers",
28+
rules: {
29+
singleProxy: {
30+
scheme: "http",
31+
host: "%s",
32+
port: %d,
33+
},
34+
bypassList: ["localhost"]
35+
}
36+
};
37+
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
38+
function callbackFn(details) {
39+
return {
40+
authCredentials: {
41+
username: "customer-%s",
42+
password: "%s",
43+
}
44+
};
45+
}
46+
chrome.webRequest.onAuthRequired.addListener(
47+
callbackFn,
48+
{urls: ["<all_urls>"]},
49+
['blocking']
50+
);
51+
"""
52+
53+
54+
def chrome_proxy(user: str, password: str, host: str, port: int, country: Optional[str] = None):
55+
if country:
56+
user = f"{user}-cc-{country}"
57+
58+
bg = BACKGROUND_JS % (
59+
host,
60+
port,
61+
user,
62+
password,
63+
)
64+
65+
ext = "proxy_extension.zip"
66+
with zipfile.ZipFile(ext, "w") as zp:
67+
zp.writestr("manifest.json", MANIFEST_JSON)
68+
zp.writestr("background.js", bg)
69+
70+
return ext

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
webdriver-manager
2+
selenium

0 commit comments

Comments
 (0)