Skip to content

Commit 67ca029

Browse files
author
oxyjonas
committed
adjust-changes
making changes to use selenium-wire instead as extensions doesnt work on regular selenium module
1 parent c81d2f7 commit 67ca029

File tree

4 files changed

+80
-119
lines changed

4 files changed

+80
-119
lines changed

README.md

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,82 @@
11
# Oxylabs’ Residential Proxies integration with Selenium
22

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)
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-wire) [<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)
44

55
## Requirements
6-
For the integration to work, you'll need to install Selenium on your system. You can do it using `pip` command:
6+
For the integration to work, you'll need to install Selenium Wire on your system as
7+
using proxy implementation with the original version of Selenium on `headless` mode
8+
doesn't work. You can do it using `pip` command:
79
```bash
8-
pip install selenium
10+
pip install selenium-wire
911
```
1012
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.
1113
```bash
1214
pip install webdriver-manager
1315
```
1416
Required version of Python: `Python 3.5` (or higher)
17+
1518
## Proxy Authentication
19+
1620
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.
1721
```python
1822
USERNAME = "your_username"
1923
PASSWORD = "your_password"
20-
HOST = "pr.oxylabs.io"
21-
PORT = 7777
22-
```
23-
Adjust the `your_username` and `your_password` values 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"
24+
ENDPOINT = "pr.oxylabs.io:7777"
2825
```
29-
To do that, adjust the `country` variable to any country that Oxylabs support.
30-
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.
26+
Adjust the `your_username` and `your_password` value fields with the username and password of
27+
your Oxylabs account.
3128

3229
## Testing Proxy Connection
30+
3331
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.
3432
```python
3533
try:
3634
driver.get("https://ip.oxylabs.io/")
37-
time.sleep(5)
35+
return f'\nYour IP is: {re.search(r"[0-9].{2,}", driver.page_source).group()}'
3836
finally:
3937
driver.quit()
4038
```
4139

4240
## Full Code
4341
```python
42+
import re
43+
from typing import Optional
4444

45-
import time
46-
from selenium import webdriver
45+
from seleniumwire import webdriver
46+
# A package to have a chromedriver always up-to-date.
4747
from webdriver_manager.chrome import ChromeDriverManager
48-
from proxies import chrome_proxy
4948

5049
USERNAME = "your_username"
5150
PASSWORD = "your_password"
52-
HOST = "pr.oxylabs.io"
53-
PORT = 7777
54-
COUNTRY = "US"
51+
ENDPOINT = "pr.oxylabs.io:7777"
5552

56-
options = webdriver.ChromeOptions()
57-
proxy_ext = chrome_proxy(USERNAME, PASSWORD, HOST, PORT, COUNTRY)
58-
options.add_extension(proxy_ext)
59-
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
6053

61-
try:
62-
driver.get("https://ip.oxylabs.io/")
63-
time.sleep(5)
64-
finally:
65-
driver.quit()
54+
def chrome_proxy(user: str, password: str, endpoint: str):
55+
wire_options = {
56+
"proxy": {
57+
"http": f"http://{user}:{password}@{endpoint}",
58+
"https": f"http://{user}:{password}@{endpoint}",
59+
}
60+
}
61+
62+
return wire_options
63+
64+
65+
def execute_driver():
66+
options = webdriver.ChromeOptions()
67+
options.headless = True
68+
proxies = chrome_proxy(USERNAME, PASSWORD, ENDPOINT)
69+
driver = webdriver.Chrome(
70+
ChromeDriverManager().install(), options=options, seleniumwire_options=proxies
71+
)
72+
try:
73+
driver.get("https://ip.oxylabs.io/")
74+
return f'\nYour IP is: {re.search(r"[0-9].{2,}", driver.page_source).group()}'
75+
finally:
76+
driver.quit()
77+
78+
79+
if __name__ == "__main__":
80+
print(execute_driver())
6681
```
6782
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: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
import time
2-
from selenium import webdriver
1+
import re
2+
from typing import Optional
3+
4+
from seleniumwire import webdriver
5+
36
# A package to have a chromedriver always up-to-date.
47
from webdriver_manager.chrome import ChromeDriverManager
5-
from proxies import chrome_proxy
68

79
USERNAME = "your_username"
810
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.quit()
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())

proxies.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
webdriver-manager
2-
selenium
2+
selenium-wire

0 commit comments

Comments
 (0)