Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72703a7

Browse files
author
oxyjonas
authored
Add files via upload
1 parent 5f10228 commit 72703a7

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

‎README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
7+
For the integration to work, you'll need to install [Selenium Wire](https://github.com/wkeeling/selenium-wire) to extend Selenium’s
8+
Python bindings as implementing proxies that require authentication using default Selenium module complicates the process too much.
9+
10+
You can do it using `pip` command:
11+
```bash
12+
pip install selenium-wire
13+
```
14+
Another required package is `webdriver-manager`. It's a package that simplifies the management of binary drivers for different browsers,
15+
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/)
16+
on pypi to find out more information.
17+
18+
You can install the following using `pip` as well:
19+
```bash
20+
pip install webdriver-manager
21+
```
22+
Required version of Python: `Python 3.5` (or higher)
23+
24+
## Proxy Authentication
25+
26+
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.
27+
```python
28+
USERNAME = "your_username"
29+
PASSWORD = "your_password"
30+
ENDPOINT = "pr.oxylabs.io:7777"
31+
```
32+
Adjust the `your_username` and `your_password` value fields with the username and password of
33+
your Oxylabs account.
34+
35+
## Testing Proxy Connection
36+
37+
To see if the proxy is working, try visiting [ip.oxylabs.io](https://ip.oxylabs.io) <br>If everything is working correctly, it will return an IP address of a proxy that you're using.
38+
```python
39+
try:
40+
driver.get("https://ip.oxylabs.io/")
41+
return f'\nYour IP is: {re.search(r"[0-9].{2,}", driver.page_source).group()}'
42+
finally:
43+
driver.quit()
44+
```
45+
46+
## Full Code
47+
```python
48+
import re
49+
from typing import Optional
50+
51+
from seleniumwire import webdriver
52+
# A package to have a chromedriver always up-to-date.
53+
from webdriver_manager.chrome import ChromeDriverManager
54+
55+
USERNAME = "your_username"
56+
PASSWORD = "your_password"
57+
ENDPOINT = "pr.oxylabs.io:7777"
58+
59+
60+
def chrome_proxy(user: str, password: str, endpoint: str):
61+
wire_options = {
62+
"proxy": {
63+
"http": f"http://{user}:{password}@{endpoint}",
64+
"https": f"http://{user}:{password}@{endpoint}",
65+
}
66+
}
67+
68+
return wire_options
69+
70+
71+
def execute_driver():
72+
options = webdriver.ChromeOptions()
73+
options.headless = True
74+
proxies = chrome_proxy(USERNAME, PASSWORD, ENDPOINT)
75+
driver = webdriver.Chrome(
76+
ChromeDriverManager().install(), options=options, seleniumwire_options=proxies
77+
)
78+
try:
79+
driver.get("https://ip.oxylabs.io/")
80+
return f'\nYour IP is: {re.search(r"[0-9].{2,}", driver.page_source).group()}'
81+
finally:
82+
driver.quit()
83+
84+
85+
if __name__ == "__main__":
86+
print(execute_driver())
87+
```
88+
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.

‎main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import re
2+
from typing import Optional
3+
4+
from seleniumwire import webdriver
5+
6+
# A package to have a chromedriver always up-to-date.
7+
from webdriver_manager.chrome import ChromeDriverManager
8+
9+
USERNAME = "your_username"
10+
PASSWORD = "your_password"
11+
ENDPOINT = "pr.oxylabs.io:7777"
12+
13+
14+
def chrome_proxy(user: str, password: str, endpoint: str):
15+
wire_options = {
16+
"proxy": {
17+
"http": f"http://{user}:{password}@{endpoint}",
18+
"https": f"http://{user}:{password}@{endpoint}",
19+
}
20+
}
21+
22+
return wire_options
23+
24+
25+
def execute_driver():
26+
options = webdriver.ChromeOptions()
27+
options.headless = True
28+
proxies = chrome_proxy(USERNAME, PASSWORD, ENDPOINT)
29+
driver = webdriver.Chrome(
30+
ChromeDriverManager().install(), options=options, seleniumwire_options=proxies
31+
)
32+
try:
33+
driver.get("https://ip.oxylabs.io/")
34+
return f'\nYour IP is: {re.search(r"[0-9].{2,}", driver.page_source).group()}'
35+
finally:
36+
driver.quit()
37+
38+
39+
if __name__ == "__main__":
40+
print(execute_driver())

‎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-wire

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /