How to Bypass Selenium Detection using Python

Hello programmers, in this tutorial we will understand selenium being detected by the web-browsers and how to bypass this detection.

Selenium is widely used in the user-interaction between web-browsers. It supports automation across different browsers which can be controlled via various different programming languages. It is considered to be one of the most suitable tools for web browser automation.

Selenium Detection

Often, when web browsers are controlled via selenium, the browser detects it. For instance, when controlling the chrome browser using selenium, the following info-bar appears:

Chrome is being controlled by automated test software.

In many cases, while controlling web pages, the webpage sends a notification for verification on the screen for the user to give some information or accept some terms and conditions. It is often sent in the form of alert boxes which don’t allow selenium to control the web-page any more. Sometimes, even a robotic captcha does not allow selenium to control the webpage.

Please check the box below to proceed
I'm not a robot

By-Pass Selenium Detection using Python

The above-discussed problems can be bypassed using another web-driver library which is the undetected chrome module.

Installation of undetected-chromedriver

Type the below command in your command prompt to download the module.

pip install undetected-chromedriver

Usage

We can by-pass the chrome info-bar message and any-other alert box messages using this library. Below given is the illustration on how to use the following module.

#Importing Necessary Libraries
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver as uc
import time

#bypass function
def seleniumUndetected():
    driver = uc.Chrome(version_main = 102) #creating a webdriver object
    driver.maximize_window() #maximize window size

    driver.get("https://www.codespeedy.com/") #opening the url
    ele = WebDriverWait(driver, 10).until( #using explicit wait for 10 seconds
        EC.presence_of_element_located((By.LINK_TEXT, "Programming Blog")) #finding the element
    )
    ele.click() #clicking on the element
    time.sleep(20)

#driver
if __name__ == "__main__":
    seleniumUndetected() #call the function

Output

The following website is opened and next the ‘Programming Blog’ page is opened.

Explanation
We import the ‘undetected_chromedriver’ module and create a web-driver object of it with ‘version_main’ as 102 because the current version installed on my computer is 102. We open the URL using the ‘.get()’ method and use an explicitly wait condition for the presence of the element and then click on the following element.

One response to “How to Bypass Selenium Detection using Python”

  1. B. Endrias says:

    is there a way to bypass without using undetected_chromedriver?

Leave a Reply

Your email address will not be published.