0

I'm trying to automate a process using selenium. Everything works perfect but the site has anti-bot methods in place which blocks my selenium script. To solve this I came across a python module called selenium-stealth. This does some stuff that avoid those anti bots. It works but the problem is that this only works on the orignal tab that gets opened in the first go. Any new tabs in that same browser doesn't have this stealth. Is there a way to add this stealth to every tab.

Here's a demo code to reproduce the stealth not working on multiple tabs:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
import time

options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument("start-maximized")
options.add_argument("--mute-audio")
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)

bot = webdriver.Chrome(service=Service("chromedriver.exe"), options=options)
stealth(bot,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        )

bot.get("https://infosimples.github.io/detect-headless/")
time.sleep(5)
bot.execute_script('''window.open("https://infosimples.github.io/detect-headless/","_blank");''')
time.sleep(20)
bot.quit()

Outputs:

Main Tab:

Main Tab

2nd Tab:

2nd Tab

As you can see, the first tab passes everything but the 2nd tab for some reason doesn't get the stealth. What could be the reason and any way to make this work?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Zen
  • 11
  • 3
  • seems like this is adjusting some values on a page load... when you open a new tab, the driver is still on the old one. (these will have different handles and are essentially like new instances of the browser) Try opening the new tab blank... switch the driver to the new tab... then use a get() to navigate to the URL. Seems like that would trigger the scripts to change the headers sent. – pcalkins Jan 25 '23 at 19:00
  • @pcalkins tried that but doesn't seem to work. I guess its a bug or something with stealth module. – Zen Jan 26 '23 at 03:55

1 Answers1

0

The fact that selenium-stealth settings aren't effective within the adjascent tabs is known issue for almost a year now.

You can find the detailed discussion within the selenium-stealth repository in Settings doesn't apply on all tabs (but only on the first one)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey thanks for pointing that out. Anyway I did find an alternative in that issue, [undetected-chromedriver](https://pypi.org/project/undetected-chromedriver/). This seem to work fine and also on multiple tabs but in my use case I have to use proxies that require authentication. I'm not sure how to get that to work with this undetected-chromedriver. I tried using extension like the top answer in this thread [stack overflow thread](https://stackoverflow.com/questions/55582136/how-to-set-proxy-with-authentication-in-selenium-chromedriver-python) but it doesn't seem to work. Any solution? Thanks – Zen Jan 26 '23 at 03:59
  • @Zen Using [undetected-chromedriver](https://stackoverflow.com/a/65534593/7429447) is all together a new question and out of scope for this question. Feel free to raise a new question with your new requirement. – undetected Selenium Feb 07 '23 at 22:17