如何使Python Selenium更不易被检测到?

huangapple go评论106阅读模式
英文:

How to make python selenium less detectable?

问题

以下是您要翻译的内容:

"Okay, so I am making a bot for this website to automate account creation. I am using rotating ipv4 proxies and some other anti detection stuff but still being detected every time. Any ideas?

Here's how I'm initializing the driver and what options I'm using.

  1. proxy = random.choice(proxys)
  2. ua = UserAgent()
  3. userAgent = ua.random
  4. def main():
  5. options = webdriver.ChromeOptions()
  6. options.add_argument(r'--user-data-dir=my chrome user data directory here')
  7. options.add_argument(f'--proxy-server={proxy}')
  8. options.add_argument("start-maximized")
  9. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  10. options.add_experimental_option('useAutomationExtension', False)
  11. options.add_argument(f'user-agent={userAgent}')
  12. driver = uc.Chrome(service=Service(ChromeDriverManager(cache_valid_range=30).install()),options=options)
  13. driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
  14. print(driver.execute_script("return navigator.userAgent;"))
  15. sleep(1)
  16. while True:
  17. create_account(driver, catchall)

I also have tried chrome profiles, but I cannot make enough of those, and was wondering how I could implement those somehow. Because it seems after the website detects I am a bot, the chrome profile I was using no longer works.

I am being blocked out every time by the website, almost no matter what. Although sometimes it does happen to go through after I make some random changes. But I was expecting everything to be working now as almost everything that they can detect is being changed."

英文:

Okay, so I am making a bot for this website to automate account creation. I am using rotating ipv4 proxies and some other anti detection stuff but still being detected every time. Any ideas?

Here's how I'm initializing the driver and what options im using.

  1. proxy = random.choice(proxys)
  2. ua = UserAgent()
  3. userAgent = ua.random
  4. def main():
  5. options = webdriver.ChromeOptions()
  6. options.add_argument(r'--user-data-dir=my chrome user data directory here')
  7. options.add_argument(f'--proxy-server={proxy}')
  8. options.add_argument("start-maximized")
  9. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  10. options.add_experimental_option('useAutomationExtension', False)
  11. options.add_argument(f'user-agent={userAgent}')
  12. driver = uc.Chrome(service=Service(ChromeDriverManager(cache_valid_range=30).install()),options=options)
  13. driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
  14. print(driver.execute_script("return navigator.userAgent;"))
  15. sleep(1)
  16. while True:
  17. create_account(driver, catchall)

I also have tried chrome profiles, but I cannot make enough of those, and was wondering how I could implement those somehow. Because it seems after the website detects I am a bot, the chrome profile I was using no longer works.

I am being blocked out every time by the website, almost no matter what. Although sometimes it does happen to go through after I make some random changes. But I was expecting everything to be working now as almost everything that they can detect is being changed.

答案1

得分: 1

selenium-stealth

为了使 Python-Selenium 几乎不可检测,最好的选择是使用 selenium-stealth,它具有以下特点:

  • 通过了所有公开的机器人测试。
  • Selenium 可以用于登录 Google 帐户。
  • 保持正常的 reCAPTCHA v3 分数

演示

  • 代码块:
  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium_stealth import stealth
  5. import time
  6. options = Options()
  7. options.add_argument("start-maximized")
  8. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  9. options.add_experimental_option('useAutomationExtension', False)
  10. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
  11. driver = webdriver.Chrome(service=s, options=options)
  12. # Selenium Stealth 设置
  13. stealth(driver,
  14. languages=["en-US", "en"],
  15. vendor="Google Inc.",
  16. platform="Win32",
  17. webgl_vendor="Intel Inc.",
  18. renderer="Intel Iris OpenGL Engine",
  19. fix_hairline=True
  20. )
  21. driver.get("https://bot.sannysoft.com")
  22. time.sleep(5)
  23. driver.save_screenshot("sannysoft.png")
  24. driver.quit()
  • 截图:

如何使Python Selenium更不易被检测到?


参考资料

您可以在以下地方找到一些相关的详细讨论:

英文:

selenium-stealth

To make Python-Selenium almost undetectable your best bet would be to use selenium-stealth which:

  • Passes all public bot tests.
  • Selenium can do google account login.
  • Maintain a normal reCAPTCHA v3 score

A demonstration

  • Code Block:

    1. from selenium import webdriver
    2. from selenium.webdriver.chrome.options import Options
    3. from selenium.webdriver.chrome.service import Service
    4. from selenium_stealth import stealth
    5. import time
    6. options = Options()
    7. options.add_argument("start-maximized")
    8. options.add_experimental_option("excludeSwitches", ["enable-automation"])
    9. options.add_experimental_option('useAutomationExtension', False)
    10. s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    11. driver = webdriver.Chrome(service=s, options=options)
    12. # Selenium Stealth settings
    13. stealth(driver,
    14. languages=["en-US", "en"],
    15. vendor="Google Inc.",
    16. platform="Win32",
    17. webgl_vendor="Intel Inc.",
    18. renderer="Intel Iris OpenGL Engine",
    19. fix_hairline=True,
    20. )
    21. driver.get("https://bot.sannysoft.com")
    22. time.sleep(5)
    23. driver.save_screenshot("sannysoft.png")
    24. driver.quit()
  • Screenshot:

如何使Python Selenium更不易被检测到?


References

You can find a couple of relevant detailed discussions in:

huangapple
  • 本文由 发表于 2023年3月4日 05:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定