undetected_chromedriver 控制已打开的浏览器

复制代码
def get_options():
    options = ChromeOptions()
    options.add_argument("--disable-blink-features=AutomationControlled")
    # options.add_experimental_option("excludeSwitches", ["enable-automation"])
    # options.add_experimental_option("useAutomationExtension", False)
    options.add_argument("--disable-extensions")
    options.add_argument("--no-default-browser-check")
    options.add_argument("--no-first-run")
    options.add_argument("--disable-popup-blocking")
    options.add_argument("--disable-default-apps")
    options.add_argument("--disable-infobars")
    # options.add_argument("--disable-gpu")
    # options.add_argument("--disable-notifications")
    options.add_argument("--disable-translate")
    options.add_argument("--disable-device-discovery-notifications")
    options.add_argument("--no-sandbox")
    # options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--window-size=1920x1080")
    options.add_argument("--disable-features=VizDisplayCompositor")
    return options

def getchromeversion():
    if 'win' in sys.platform:
        import winreg
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
        chrome_version = winreg.QueryValueEx(key, 'version')[0]
        return chrome_version
    else:
        from webdriver_manager.utils import get_browser_version_from_os
        browserVersion = get_browser_version_from_os("google-chrome")  # 获取当前系统chrome浏览器的版本号
        print(f'Chrome version is {browserVersion}')
        mainBrowserVersion = browserVersion.split(".")[0]  # 获取chrome浏览器的主版本号
        return browserVersion


def get_session_url(port):
    url = f'http://127.0.0.1:{port}/json/version'
    try:
        res = requests.get(url)
        print(res.text)
        webSocketDebuggerUrl = json.loads(res.text)['webSocketDebuggerUrl']
    except:
        # 命令行启动
        time.sleep(random.randint(4, 6))


def start_brower():
    from undetected_chromedriver import Chrome, ChromeOptions, find_chrome_executable
    user_data_name = "chrome1"
    port = 9315
    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
    current_path = os.path.dirname(os.path.realpath(sys.argv[0]))
    # 浏览器用户配置文件
    user_data_dir = os.path.join(current_path, user_data_name)
    # 浏览器版本号
    version = getchromeversion().split('.')[0]
    # 下载驱动
    currentpath = os.path.dirname(os.path.realpath(sys.argv[0]))
    executable_path = os.path.join(currentpath, 'chromedriver_win32', f'chromedriver_{version}.exe')
    if not os.path.exists(executable_path):
        # get_chromedriver_version(path, version)
        print('自动下载驱动')
    # 浏览器路径
    chrome_path = find_chrome_executable()
    print('executable_path:', executable_path)
    print('user_data_dir:', user_data_dir)
    print('chrome_path:', chrome_path)

    option = get_options()
    option.add_argument(fr'--user-data-dir="{user_data_dir}"')
    option.add_argument(f"--user-agent={user_agent}")
    option.debugger_address = f"127.0.0.1:{port}"
    driver = Chrome(options=option, executable_path=executable_path)
    print(driver.title)
    time.sleep(2)
    driver.get('https://blog.csdn.net/lwdfzr/article/details/135410359')
    time.sleep(random.randint(3, 5))
复制代码
undetected_chromedriver 通过端口控制已打开的浏览器时,会打开一个多余的空白窗口,

鼠标放在Chrome() 按ctrl+B查看源码,找到这段代码,注释掉

相关推荐
Blossom.11818 小时前
从“能写”到“能干活”:大模型工具调用(Function-Calling)的工程化落地指南
数据库·人工智能·python·深度学习·机器学习·计算机视觉·oracle
蒋星熠19 小时前
破壁者指南:内网穿透技术的深度解构与实战方法
网络·数据库·redis·python·websocket·网络协议·udp
shizidushu19 小时前
使用 Pyinstaller 打包 PPOCRLabel
python·pyinstaller
Q_Q196328847519 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
Rhys..20 小时前
.gitignore文件的作用及用法
python·github
IT学长编程20 小时前
计算机毕业设计 基于深度学习的酒店评论文本情感分析研究 Python毕业设计项目 Hadoop毕业设计选题 机器学习选题【附源码+文档报告+安装调试】
hadoop·python·深度学习·机器学习·数据分析·毕业设计·酒店评论文本情感分析
~-~%%21 小时前
Moe机制与pytorch实现
人工智能·pytorch·python
深耕AI21 小时前
【PyTorch训练】为什么要有 loss.backward() 和 optimizer.step()?
人工智能·pytorch·python
0_0梅伊阁诗人21 小时前
Django ORM 模型
开发语言·数据库·笔记·python·oracle·django
Genevieve_xiao1 天前
【dl】python基础 深度学习中需要用到的python基础
python·深度学习