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

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

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.

proxy = random.choice(proxys)
ua = UserAgent()
userAgent = ua.random

def main():
    options = webdriver.ChromeOptions()
    options.add_argument(r'--user-data-dir=my chrome user data directory here')
    options.add_argument(f'--proxy-server={proxy}')
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument(f'user-agent={userAgent}')
    driver = uc.Chrome(service=Service(ChromeDriverManager(cache_valid_range=30).install()),options=options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    print(driver.execute_script("return navigator.userAgent;"))
    sleep(1)
    while True:
        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.

proxy = random.choice(proxys)
ua = UserAgent()
userAgent = ua.random

def main():
    options = webdriver.ChromeOptions()
    options.add_argument(r'--user-data-dir=my chrome user data directory here')
    options.add_argument(f'--proxy-server={proxy}')
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    options.add_argument(f'user-agent={userAgent}')
    driver = uc.Chrome(service=Service(ChromeDriverManager(cache_valid_range=30).install()),options=options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    print(driver.execute_script("return navigator.userAgent;"))
    sleep(1)
    while True:
        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 分数

演示

  • 代码块:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
import time
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

# Selenium Stealth 设置
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://bot.sannysoft.com")
time.sleep(5)
driver.save_screenshot("sannysoft.png")
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:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from selenium_stealth import stealth
    import time
    options = Options()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    driver = webdriver.Chrome(service=s, options=options)
    
    # Selenium Stealth settings
    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://bot.sannysoft.com")
    time.sleep(5)
    driver.save_screenshot("sannysoft.png")
    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:

确定