seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.2k stars 964 forks source link

How to switch to proxy using selenium wire #3048

Closed Damji7 closed 1 month ago

Damji7 commented 1 month ago

hello, How to switch to proxy using selenium wire to performe some actions and then it rotats back to the same ip, by the way my script is using the uc mode in simple sb form.

i want to execute proxy in the same browser session to perform some actions when certain text is visible in the webpage, and the it rotats back to the original ip. is it prossible if so can you help with it.

here is my code snippet:

with SB(uc=True, incognito=True, locale_code='en', wire=True) as sb: def open_the_webpage(sb): url = "web page" sb.uc_open_with_reconnect(url, 2)

while sb.is_text_visible('welcome', 'h1'):
    try:
        sb.uc_gui_click_captcha()
    except Exception as e:
        print(f"Error handling captcha: {e}")

    sb.sleep(5)

    if not sb.is_text_visible('welcome', 'h1'):
        print("Captcha successfully passed.")
        break

    print("Captcha not yet passed, retrying...")

if sb.is_text_visible("You are being rate limited", "h2"):
    switch_proxy(sb) #so i can call it here 

def switch_proxy(sb):

how to use seleniumwire here

thanks and regards <3

mdmintz commented 1 month ago

Duplicate of https://github.com/seleniumbase/SeleniumBase/issues/1774#issuecomment-1444399269, https://github.com/seleniumbase/SeleniumBase/issues/2032#issuecomment-1693931853, https://github.com/seleniumbase/SeleniumBase/issues/2151#issuecomment-1741234068, etc.

Due to unresolved issues with selenium-wire: https://github.com/wkeeling/selenium-wire/search?q=undetected&type=issues, UC Mode and Wire Mode cannot be combined. Pick one or the other.


If you want to change your proxy with Wire Mode (without using UC Mode), here's an example:

from seleniumbase import Driver

driver = Driver(wire=True)
try:
    driver.set_wire_proxy("user:pass@ip:port")
    driver.open("https://httpbin.io/ip")
    driver.sleep(6)
finally:
    driver.quit()

For the SB() format, there's sb.driver.set_wire_proxy("user:pass@ip:port")


If you need UC Mode, then set the proxy arg to use a proxy:

https://github.com/seleniumbase/SeleniumBase/blob/af3d9545473e55b2a25cdbab8be0b1ed5e1f6afa/seleniumbase/plugins/sb_manager.py#L41

Damji7 commented 1 month ago

i want to use the uc mode and when the script finds certain text in the webpage it execute the proxy perform some action and then it rotats to the original ip, is it possible if so can you help ?

mdmintz commented 1 month ago

You have to decide which proxy to use when you spin up your web browser. After the browser has been spun up, you have to launch a new browser if you want different proxy settings.

Damji7 commented 1 month ago

so it is impossible to execute the proxy in the same browser session, and there is no possible way to make the proxy dynamically changes when i use the uc mode, right ?

mdmintz commented 1 month ago

You can't change your proxy in the middle of a browser session when using UC Mode.

Damji7 commented 1 month ago

Well, Its a BIG PROBLEM HERE, btw Thanks&Regards