Skip to content

Is there any method in combination with selenium-stealth ? #942

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

Closed
thanhvodad opened this issue Jul 15, 2021 · 9 comments
Closed

Is there any method in combination with selenium-stealth ? #942

thanhvodad opened this issue Jul 15, 2021 · 9 comments
Labels
question Someone is looking for answers

Comments

@thanhvodad
Copy link

https://pypi.org/project/selenium-stealth/
It is a library that combines with selenium to stealth over google, is there any way to integrate it into seleniumbase, it's very good

@mdmintz
Copy link
Member

mdmintz commented Jul 15, 2021

By the looks of it, you can integrate it directly into the custom setUp() method of your tests like this after you pip install it:

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,
        )

It's that simple. No extra code is required on the SeleniumBase side.

@mdmintz mdmintz added the question Someone is looking for answers label Jul 15, 2021
@thanhvodad
Copy link
Author

By the looks of it, you can integrate it directly into the custom setUp() method of your tests like this after you pip install it:

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,
        )

It's that simple. No extra code is required on the SeleniumBase side.

How can I choose my custom chromedriver file?.

@mdmintz
Copy link
Member

mdmintz commented Jul 15, 2021

@thanhvodad The chromedriver used is the one that's on your system path. So if you want to use a specific chromedriver, make sure that it's the only one on the path. SeleniumBase adds the seleniumbase/drivers folder to your path when you run tests, so any driver installed there via a command such as sbase install chromedriver latest would be used before any other one that's found elsewhere. Maybe do a search for chromedriver to see all occurrences of it on your computer in case you're not sure which ones exist.

@thanhvodad
Copy link
Author

def setUp(self):
    if self.browser == "chrome":
        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,
        )

object has no attribute 'browser'
self.browser not in setup ?

@mdmintz
Copy link
Member

mdmintz commented Jul 16, 2021

@thanhvodad You can call the self commands in your setUp() method only after you've run super().setUp(), which is on the very next line in your example.

@thanhvodad
Copy link
Author

what will be the correct command, when i want to check if it's browe.chorme, if yes i will use stealth

@mdmintz
Copy link
Member

mdmintz commented Jul 16, 2021

@thanhvodad

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

(Make sure you call super().setUp() before you call any self commands.)

@thanhvodad
Copy link
Author

I tried, yes seleniumbase has been caught by google automatically, I login google it will say :(

@mdmintz
Copy link
Member

mdmintz commented Jul 16, 2021

@thanhvodad If you're looking to avoid Selenium detection, you should see https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver/52108199#52108199 and the solution for that here: https://stackoverflow.com/a/52108199

In summary, you'll have to modify chromedriver so that websites don't know how to detect it anymore. Here's how you can do that with vim:
vim /path/to/chromedriver

After running the line above, you'll probably see a bunch of gibberish. Do the following:

  1. Replace all instances of cdc_ with dog_ by typing :%s/cdc_/dog_/g.
    • dog_ is just an example. You can choose anything as long as it has the same amount of characters as the search string (e.g., cdc_), otherwise the chromedriver will fail.
  2. To save the changes and quit, type :wq! and press return.
    • If you need to quit without saving changes, type :q! and press return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Someone is looking for answers
Projects
None yet
Development

No branches or pull requests

2 participants