Home > Blockchain >  How to load chrome options using undetected chrome
How to load chrome options using undetected chrome

Time:12-14

So Im trying to modify the user agent of the chrome driven by undetected chrome to pretend I am a mobile user. it works with usual chrome driver but not the undetected one, I don't sure what I have to change to make it work for undetected chrome, here is the code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import undetected_chromedriver.v2 as uc
options = uc.ChromeOptions()
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")
# s = Service('./chromedriver')
# driver = webdriver.Chrome(options=options, service=s)
driver = uc.Chrome(options=options)
driver.get('https://www.google.com')
print(driver.execute_script("return navigator.userAgent;"))
time.sleep(1000000)

So if use the undetected chrome, the user agent printed is:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

but it is working if i use the regular chrome driver

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36

CodePudding user response:

The script below has an example of all those things using SeleniumBase:

(After pip install seleniumbase and running the script with python)

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://nowsecure.nl/#relax")
    try:
        sb.assert_text("OH YEAH, you passed!", "h1", timeout=8.75)
        sb.post_message("Selenium wasn't detected!", duration=1.6)
        sb._print("\n Success! Website did not detect Selenium! \n")
    except Exception:
        sb.fail('Selenium was detected!\n')

    sb.open("data:,")

    sb.driver.execute_cdp_cmd(
        "Network.setUserAgentOverride",
        {
            "userAgent": "Mozilla/5.0 "
            "(Windows NT 10.0; Win64; x64) "
            "AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/83.0.4103.106 Safari/537.36"
        },
    )

    user_agent = sb.execute_script("return navigator.userAgent;")
    print(user_agent)

    sb.set_messenger_theme(theme="flat", location="top_center")
    sb.post_message(user_agent, duration=4)

The first part of the script verifies that SeleniumBase's Undetected Chromedriver mode is working correctly. The next part uses sb.driver.execute_cdp_cmd to change the user agent, print the output, and display the user agent in the browser.

  • Related