2

I am having trouble connecting to this proxy with seleniumwire, it will connect and ask me for user and password if I remove this line. and yes, I have the actual directory and proxy in the real code, it is changed on here for privacy reasons.

chrome_options.add_argument(f'--profile-directory={profile_directory}')

I'm not sure if maybe the profile directory changes proxy or what but it will only connect if that is removed, how can I make it connect and still use the profile directory?

from seleniumwire import webdriver

proxy_address = "your_proxy_address:your_proxy_port"
user_data_dir = "path/to/your/user/data/directory"
profile_directory = "YourProfileDirectory"

chrome_options = webdriver.ChromeOptions()

proxy_options = {
    'proxy': {
        'http': f'http://{proxy_address}',
        'https': f'https://{proxy_address}',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
chrome_options.add_argument(f'--profile-directory={profile_directory}')

driver = webdriver.Chrome(seleniumwire_options=proxy_options, chrome_options=chrome_options)


driver.get("https://myurl.com/")
3
  • are you using proxies with IP authorization? If not, have you tried this? Commented Nov 23, 2023 at 8:33
  • I have tried both proxy with auth and no auth, it simply won't connect if im adding the profile directory. Commented Nov 23, 2023 at 22:37
  • Can you try to run chrome on no-securitymode Try this chrome.exe --user-data-dir="C:/tmp/chrome_dev_test" --disable-web-security -- Commented Nov 28, 2023 at 17:44

4 Answers 4

3
+25

Is your proxy 100% valid? Or maybe the website doesn't allow connection through this particular proxy?

Just tried your code with my proxy (authentication by login and password, login:password@ip:proxy) and it works perfectly.

import time
from seleniumwire import webdriver

proxy_address = "login:password@ip:proxy"
user_data_dir = r"C:\Users\USERNAME\Desktop\stack\userdata"
profile_directory = "Profile 2"

chrome_options = webdriver.ChromeOptions()

proxy_options = {
    'proxy': {
        'http': f'http://{proxy_address}',
        'https': f'https://{proxy_address}',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

chrome_options.add_argument(f'--user-data-dir={user_data_dir}')
chrome_options.add_argument(f'--profile-directory={profile_directory}')

driver = webdriver.Chrome(seleniumwire_options=proxy_options, chrome_options=chrome_options)

driver.get("http://api.ipify.org/")

print("Waiting...")
time.sleep(10)
Sign up to request clarification or add additional context in comments.

Comments

0
  • Give user-data directory path as follows -

C:\Users\<username>\AppData\Local\Google\Chrome\User Data

  • Then add profile directory argument as the name of the profile.
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\Users\<username>\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"profile-directory=Profile 1")

3 Comments

No, it still won't connect to the proxy, it doesn't event try to connect...
Are there any errors?
there are no errors
0

I ve passed throw such problem, for me it have been because I've made an extension that change whether how proxies works, if you ve made something like this, then please disable this extension check if your proxy work, and if not, check if its work if you add:

chrome_options.add_argument("--disable-extensions")

If it's work with this argument then problem in extensions that after it's disabling still use chrome proxy settings. And easy solution is to rename Preferences.txt file in profile directory for example to Preferences_main.txt and then you can try to turn off disable extensions argument.

Comments

-1

I just love Python and i have used Python so so much in my projects so here are some improvements and additional considerations

Proxy Authentication: If your proxy requires authentication, you'll need to include the username and password in the proxy settings. Selenium Wire supports this with the following format:

proxy_options = {
    'proxy': {
        'http': f'http://{username}:{password}@{proxy_address}',
        'https': f'https://{username}:{password}@{proxy_address}',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

Chrome Driver Options: chrome_options is deprecated in favor of options. It’s a good practice to use the updated terminology:

options = webdriver.ChromeOptions()

Check for Missing -- in Arguments: Ensure that all Chrome arguments start with --. Sometimes missing hyphens can cause issues.

Explicit Proxy Authentication: If you continue to face issues with proxy authentication, consider using an extension that can handle proxy authentication or handle the authentication popup using Selenium.

Logging for Debugging: Add logging to help diagnose issues. Selenium Wire allows you to see the requests made by the browser:

from seleniumwire import webdriver

# Enable logging
webdriver.Chrome(seleniumwire_options={'enable_logging': True})

WebDriver Path: Ensure you have specified the path to your WebDriver if it's not in your PATH environment variable:

driver = webdriver.Chrome(executable_path='path/to/chromedriver', seleniumwire_options=proxy_options, options=options)

Error Handling: Include error handling to manage exceptions that may occur when the browser is launched or when navigating to a URL.

Close WebDriver Properly: Ensure that the WebDriver is properly closed after its use to avoid any resource leaks.

try:
    driver.get("https://myurl.com/")
    # Your code...
finally:
    driver.quit()

Profile Path Verification: Double-check the validity of user_data_dir and profile_directory. Incorrect paths can cause Chrome to start with default settings, ignoring specified profiles.

WebDriver Wait: For dynamic pages, consider using explicit waits to ensure that the elements you interact with are loaded.

Headless Mode: For testing or running in a server environment, you might want to run Chrome in headless mode:

options.add_argument("--headless")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.