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查看源码,找到这段代码,注释掉

相关推荐
chilavert31832 分钟前
关于Python 实现接口安全防护:限流、熔断降级与认证授权的深度实践
python·网络安全
能来帮帮蒟蒻吗1 小时前
Python -将MP4文件转为GIF图片
开发语言·python·学习·视频
suoxiao7771 小时前
通过anaconda安装jupyter
ide·python·jupyter
百锦再1 小时前
MK米客方德SD NAND:无人机存储的高效解决方案
人工智能·python·django·sqlite·android studio·无人机·数据库开发
PacosonSWJTU1 小时前
python使用matplotlib画图
开发语言·python·matplotlib
伶俐角少儿编程1 小时前
2023年12月中国电子学会青少年软件编程(Python)等级考试试卷(六级)答案 + 解析
python·青少年编程·少儿编程·中国电子学会等级考试·中国电子学会
tangjunjun-owen2 小时前
Milvus 2.4 使用详解:从零构建向量数据库并实现搜索功能(Python 实战)
数据库·python·milvus·rag
CryptoRzz2 小时前
印度尼西亚数据源对接技术指南
开发语言·python·websocket·金融·区块链
戌崂石2 小时前
最优化方法Python计算:有约束优化应用——线性可分问题支持向量机
python·机器学习·支持向量机·最优化方法
灏瀚星空4 小时前
基于Python的量化交易实盘部署与风险管理指南
开发语言·python