Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting the selenium-stealth integration with UC Mode #2404

Open
mdmintz opened this issue Dec 31, 2023 · 14 comments
Open

Documenting the selenium-stealth integration with UC Mode #2404

mdmintz opened this issue Dec 31, 2023 · 14 comments
Assignees
Labels
documentation UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@mdmintz
Copy link
Member

mdmintz commented Dec 31, 2023

Documenting the selenium-stealth integration with UC Mode

(Info: selenium-stealth lets you mask your web browser's fingerprint and WebGL information.)

Prerequisites: selenium-stealth must be installed separately: pip install selenium-stealth

Here's an example of the selenium-stealth integration with the Driver() manager and UC Mode:

from seleniumbase import Driver
from selenium_stealth import stealth

driver = Driver(uc=True)
stealth(
    driver,
    languages=["en-US", "en"],
    vendor="Google Inc.",
    platform="Win32",
    webgl_vendor="Intel Inc.",
    renderer="Intel Iris OpenGL Engine",
    fix_hairline=True,
)
driver.get("https://browserleaks.com/webrtc")
driver.sleep(10)
driver.quit()

selenium-stealth also integrates with SeleniumBase BaseCase formats:

Use Syntax Format 2 to override the setUp() method so that selenium-stealth settings are used:

from seleniumbase import BaseCase
from selenium_stealth import stealth

class BaseTestCase(BaseCase):
    def setUp(self):
        super().setUp()
        stealth(self.driver,
            languages=["en-US", "en"],
            vendor="Google Inc.",
            platform="Win32",
            webgl_vendor="Intel Inc.",
            renderer="Intel Iris OpenGL Engine",
            fix_hairline=True,
        )

Then have your test classes inherit BaseTestCase instead of BaseCase.
(See SeleniumBase/help_docs/syntax_formats.md#sb_sf_02 for details.)

To activate UC Mode in BaseCase formats, use --uc as a pytest command-line option:

pytest --uc

For general information about selenium-stealth, see:


For general information about UC Mode, see #2213

@mdmintz mdmintz added documentation UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Dec 31, 2023
@mdmintz mdmintz self-assigned this Dec 31, 2023
@AlexPaiva
Copy link

Stealth hasn't been updated in a while, is it still viable?

@mdmintz
Copy link
Member Author

mdmintz commented Jan 3, 2024

@AlexPaiva Some people are still using it, so it's probably still valid, even though it hasn't been updated in awhile.

But at this point, selenium-stealth is mainly just for fingerprint-masking.
The part with avoiding selenium-detection is fully being done by seleniumbase UC Mode.

@xipeng5

This comment was marked as off-topic.

@xipeng5

This comment was marked as off-topic.

@kitsune0n
Copy link

kitsune0n commented Jun 25, 2024

how use stealth with SB(from seleniumbase import SB) and uc=True? adding

stealth(sb.driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
       )

don't works

@mdmintz
Copy link
Member Author

mdmintz commented Jun 25, 2024

@kitsune0n If the driver needs to disconnect (eg, a uc_open method is called) or if going to a site with a Cloudflare CAPTCHA (eg. via driver.get(), where the disconnect is automatic) then selenium-stealth settings are reset. Unless the maintainer of https://github.com/diprajpatra/selenium-stealth makes changes, there's nothing I can do from my end to help.

Also, selenium-stealth is not needed to bypass CAPTCHAs: Regular seleniumbase UC Mode can do that alone.

@PankajSavaliya
Copy link

PankajSavaliya commented Jul 5, 2024

I have used selenium-stealth, but in UC mode, there is always a need for connect and reconnect methods. Do I need to apply stealth each time? because as you said it resets if I use uc_open or uc_click or any disconnect method use. Is there any way to always attach again and all the info with any customize?

 with SB(uc=True) as sb:
        stealth(
            sb.driver,
            user_agent=device.get('user_agent'),
            languages=["en-US", "en"],
            vendor=device.get('vendor'),
            platform=device.get('platform'),
            webgl_vendor=device.get('webgl_vendor'),
            renderer=device.get('renderer'),
            fix_hairline=device.get('fix_hairline'),
        )
        sb.set_window_size(width=device.get('width'), height=device.get('height'))
        sb.uc_open('https://amiunique.org/')
        time.sleep(50)

In SB there is agent set method but not for timezone, we can set the timezone external raw script but it reset while disconnecting the driver, please let me know if there is a change to customize SB to override the method of reconnect, as I have used SB context-manager base so I need to customize uc_open, uc_click and reconnect method so I can execute timezone method each time

sb.execute_cdp_cmd('Emulation.setTimezoneOverride', tz_params)

@mdmintz
Copy link
Member Author

mdmintz commented Jul 5, 2024

#2856 (comment):

When UC Mode disconnects the driver from Chrome to evade detection, it also resets any settings made by selenium-stealth.

Also, it appears that using selenium-stealth to modify settings will get detected as a possible attempt to mask your fingerprint. (There is little that I can do there, as that is an external library.)

Your best bet to avoid detection is by just using regular SeleniumBase UC Mode:

from seleniumbase import SB

with SB(uc=True, incognito=True) as sb:
    sb.uc_open_with_reconnect("https://pixelscan.net/", 10)
    breakpoint()

If you want timezone changes to be permanent, use a Chrome extension that changes your timezone.

@PankajSavaliya
Copy link

#2856 (comment):

When UC Mode disconnects the driver from Chrome to evade detection, it also resets any settings made by selenium-stealth.

Also, it appears that using selenium-stealth to modify settings will get detected as a possible attempt to mask your fingerprint. (There is little that I can do there, as that is an external library.)

Your best bet to avoid detection is by just using regular SeleniumBase UC Mode:

from seleniumbase import SB

with SB(uc=True, incognito=True) as sb:
    sb.uc_open_with_reconnect("https://pixelscan.net/", 10)
    breakpoint()

If you want timezone changes to be permanent, use a Chrome extension that changes your timezone.

Yes, I want to change the timezone permanently, I have already removed that library because it's not updated.

Can you please how to do with extension with SB? just give me a hint as we can install dynamically in driver but how? @mdmintz

@mdmintz
Copy link
Member Author

mdmintz commented Jul 5, 2024

@PankajSavaliya For changing the timezone, you'll have to find an extension that does it. I hear they exist, but I don't know one off the top of my head directly.

@PankajSavaliya
Copy link

How to add the extension in SB if I have an extension so how to give the path of extension to SB? @mdmintz

@mdmintz
Copy link
Member Author

mdmintz commented Jul 5, 2024

Adding an extension:

extension_dir=None, # Load a Chrome Extension directory, comma-separated.

@PankajSavaliya

This comment was marked as off-topic.

@PankajSavaliya

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

No branches or pull requests

5 participants