|
1 | 1 | # Oxylabs’ Residential Proxies integration with Selenium |
2 | 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) |
| 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) |
4 | 4 |
|
5 | 5 | ## 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: |
7 | 9 | ```bash |
8 | | -pip install selenium |
| 10 | +pip install selenium-wire |
9 | 11 | ``` |
10 | 12 | 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 | 13 | ```bash |
12 | 14 | pip install webdriver-manager |
13 | 15 | ``` |
14 | 16 | Required version of Python: `Python 3.5` (or higher) |
| 17 | + |
15 | 18 | ## Proxy Authentication |
| 19 | + |
16 | 20 | 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 | 21 | ```python |
18 | 22 | USERNAME = "your_username" |
19 | 23 | 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" |
28 | 25 | ``` |
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. |
31 | 28 |
|
32 | 29 | ## Testing Proxy Connection |
| 30 | + |
33 | 31 | 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. |
34 | 32 | ```python |
35 | 33 | try: |
36 | 34 | 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()}' |
38 | 36 | finally: |
39 | 37 | driver.quit() |
40 | 38 | ``` |
41 | 39 |
|
42 | 40 | ## Full Code |
43 | 41 | ```python |
| 42 | +import re |
| 43 | +from typing import Optional |
44 | 44 |
|
45 | | -import time |
46 | | -from selenium import webdriver |
| 45 | +from seleniumwire import webdriver |
| 46 | +# A package to have a chromedriver always up-to-date. |
47 | 47 | from webdriver_manager.chrome import ChromeDriverManager |
48 | | -from proxies import chrome_proxy |
49 | 48 |
|
50 | 49 | USERNAME = "your_username" |
51 | 50 | PASSWORD = "your_password" |
52 | | -HOST = "pr.oxylabs.io" |
53 | | -PORT = 7777 |
54 | | -COUNTRY = "US" |
| 51 | +ENDPOINT = "pr.oxylabs.io:7777" |
55 | 52 |
|
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) |
60 | 53 |
|
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()) |
66 | 81 | ``` |
67 | 82 | 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. |
0 commit comments