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:
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?