Skip to content

Selenium ​

What is Selenium? ​

Selenium is an umbrella project for a series of tools and libraries that enable and support the automation of web browsers.

Selenium has many features, but at its core it is a toolset for web browser automation, which uses the best technologies available to remotely control browser instances and simulate user interactions with the browser.

Here's how to integrate Socks5 with Selenium.

1

Install Selenium ​

Install Selenium Wire to extend Selenium's Python bindings, as using the default Selenium modules to implement proxies that require authentication complicates the process. You can do this using pip command: pip install selenium-wire

Another recommended package for this integration is webdriver-manager. It is a suite that simplifies the management of binary drivers for different browsers. In this case, there is no need to manually download a new version of the web driver after each update.

2

Specify your user credentials for the agent to function properly ​

USERNAME = "socks5-customer-USERNAME"

PASSWORD = "PASSWORD"

ENDPOINT = "proxy-as.socks5.io:3000"

3

Check proxy status ​

Check whether the agent is functioning properly by visiting https://ip234.in/ip.json. If everything is fine - it will return the IP address of the agent you are using.

try:
    driver.get("https://ip234.in/ip.json/")
    return f'\nYour IP is: {driver.find_element(By.CSS_SELECTOR, "pre").text}'
finally:
    driver.quit()

Full code example:

from selenium.webdriver.common.by import By
from seleniumwire import webdriver
# A package to have a chromedriver always up-to-date.
from webdriver_manager.chrome import ChromeDriverManager

USERNAME = "socks5-customer-USERNAME"
PASSWORD = "PASSWORD"
ENDPOINT = "proxy-as.socks5.io:3000"

def chrome_proxy(user: str, password: str, endpoint: str) -> dict:
    wire_options = {
        "proxy": {
            "http": f"http://{user}:{password}@{endpoint}",
            "https": f"https://{user}:{password}@{endpoint}",
        }
    }

    return wire_options

def execute_driver():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    proxies = chrome_proxy(USERNAME, PASSWORD, ENDPOINT)
    driver = webdriver.Chrome(
    ChromeDriverManager(driver_version='<VERSION>').install(), options=options, seleniumwire_options=proxies
    )
    try:
        driver.get("https://ip234.in/ip.json/")
        return f'\nYour IP is: {driver.find_element(By.CSS_SELECTOR, "pre").text}'
    finally:
        driver.quit()


if __name__ == '__main__':
    print(execute_driver())