|
| 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. |
0 commit comments