Skip to content

无法登录deepl #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
D2epsl2ep opened this issue Sep 2, 2024 · 2 comments
Closed

无法登录deepl #5

D2epsl2ep opened this issue Sep 2, 2024 · 2 comments

Comments

@D2epsl2ep
Copy link

image

@infrost
Copy link
Owner

infrost commented Sep 3, 2024

感谢反馈该问题,这确实是一个已知的问题。原因应该出在cloudflare的验证机制对playwright的webdriver的特征检测上。对于playwright的webkit内核,也就是目前默认的方法上,没有特别好的解决办法,因为那个内核是微软高度定制的很难绕过自动化特征检测,playwright本身对反爬虫检测的手段相比其他自动化测试软件如selenium等也少一些。
对于playwright调用的edge内核的deepl登录错误,已经找到了解决方法,具体来说是让playwright拉起带用户数据的真实edge浏览器来操作。但是edge浏览器的自动化翻译支持目前说实话还不够好,所以可能需要等待我连带着把其他几个问题一起修复后合并更新,你也可以更新playwright_process.pyplaywright_engine函数的部分代码,登陆问题会被修复:

    # 构建 Edge 默认用户数据目录路径
    username = os.getlogin()
    if os.name == 'nt':  # Windows
        user_data_dir = fr"C:\Users\{username}\AppData\Local\Microsoft\Edge\User Data"
    elif os.name == 'posix':  # macOS/Linux
        if 'darwin' in os.uname().sysname.lower():  # macOS
            user_data_dir = f"/Users/{username}/Library/Application Support/Microsoft Edge"
        else:  # Linux
            user_data_dir = f"/home/{username}/.config/microsoft-edge"
    else:
        raise OSError("Unsupported operating system")
    if not os.path.exists(user_data_dir):
        raise FileNotFoundError(f"用户数据目录不存在: {user_data_dir}")
    subprocess.Popen([browser_executable_path, '--remote-debugging-port=9222', f'--user-data-dir={user_data_dir}'], shell = True)
    with sync_playwright() as p:
        # Launch the browser
        print("正在拉起浏览器")
        if Path(playwright_path).exists():
            browser_executable_path = Path(playwright_path)
            browser = p.webkit.launch(executable_path=browser_executable_path, headless=playwright_headless)
            make_edge_happy = False
        elif default_edge_path.exists():
            print("未找到webikit内核,使用edge内核,但该模式可能兼容性有问题")
            browser = p.chromium.connect_over_cdp('http://127.0.0.1:9222')  # 连接到远程调试端口
            context = browser.contexts[0]  # 获取已有的上下文
            make_edge_happy = True
        else:
            raise FileNotFoundError("找不到Playwright内核")
        page = context.pages[0]

注意需要彻底关闭正在运行的edge才能生效。

@D2epsl2ep
Copy link
Author

好的,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants