selenium-webdrivercloudflare

Python script for launching Chrome browser


I am working on a python script to launch chrome browser as well as to bypass cloudfare bot detection (captcha,java rendering, etc.). I am using the script below but chrome is not getting launched.

pip install  undetected-chromedriver

import undetected_chromedriver as uc

def open_webpage(url):
# Set Chrome Options
options = uc.ChromeOptions()

# Switch Undetected ChromeDriver to Headless Mode
options.add_argument('--headless')  # Correct argument for headless mode
options.add_argument('--user-agent=Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36')

# Proxy settings: Specify your proxy address and port
proxy_address = "socks://username:password@proxyserver:port"
options.add_argument(f'--proxy-server={proxy_address}')

# Create a Chrome browser instance with undetected-chromedriver
driver = uc.Chrome(options=options)

# Fetch the current user agent to verify
current_user_agent = driver.execute_script("return navigator.userAgent;")
print("Current User Agent:", current_user_agent)

# Open the specified URL
driver.get(url)

open_webpage('https://www.indeed.com')

"


Solution

  • First, import the webbrowser using import webbrowser There are multiple was to do it, but the easiest one is

    webbrowser.open_new_tab("https://www.stackoverflow.com")
    

    This opens the target page in a new tab if there is a browser already, else it should open a new standart browser window. Here is the documentation, including some other ways to do it that have different results, but this one should do the trick for you.

    A working program could look like this:

    import webbrowser
    
    userinput = input("Enter your website: ")
    webbrowser.open_new_tab(userinput)
    print("Opened " + userinput)