V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dawnzhu
V2EX  ›  Python

pyppeteer page.type 输入中文报错

  •  
  •   dawnzhu · 2020-12-29 15:22:40 +08:00 · 1571 次点击
    这是一个创建于 1420 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- coding: utf-8 -*-
    
    import asyncio
    from pyppeteer import launch
    
    
    async def main():
        browser = await launch({'headless': False})
        page = await browser.newPage()
        await page.goto('https://app.yinxiang.com/Login.action', {"timeout": 14 * 60000})
        await page.type("#username", 'test123 哒哒哒')
    
    asyncio.get_event_loop().run_until_complete(main())
    

    打印信息

    input options {}
    char step--> t
    if char key_definitions t
    char step--> e
    if char key_definitions e
    char step--> s
    if char key_definitions s
    char step--> t
    if char key_definitions t
    char step--> 1
    if char key_definitions 1
    char step--> 2
    if char key_definitions 2
    char step--> 3
    if char key_definitions 3
    char step--> 哒
    else char --> 哒
    Traceback (most recent call last):
      File "F:/code_zhu/PyCode/py_work/Alibaba/tamll_spider/tamll_spider.py", line 14, in <module>
        asyncio.get_event_loop().run_until_complete(main())
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 568, in run_until_complete
        return future.result()
      File "F:/code_zhu/PyCode/py_work/Alibaba/tamll_spider/tamll_spider.py", line 11, in main
        await page.type("#username", 'test123 哒哒哒')
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pyppeteer\page.py", line 1589, in type
        return await frame.type(selector, text, options, **kwargs)
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pyppeteer\frame_manager.py", line 661, in type
        await handle.type(text, options)
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pyppeteer\element_handle.py", line 217, in type
        await self._page.keyboard.type(text, options)
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pyppeteer\input.py", line 217, in type
        await self.sendCharacter(char)
      File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pyppeteer\input.py", line 186, in sendCharacter
        await self._client.send('Input.insertText', {'text': char})
    pyppeteer.errors.NetworkError: Protocol error (Input.insertText): 'Input.insertText' wasn't found
    
    Process finished with exit code 1
    

    根据报错信息找到 input.py 文件

        async def type(self, text: str, options: Dict = None, **kwargs: Any
                       ) -> None:
            options = merge_dict(options, kwargs)
            print("input options", options)  # 做的参数打印信息
            delay = options.get('delay', 0)
            for char in text:
                print("char step-->", char)
                if char in keyDefinitions:
                    print("if char key_definitions", char)  # 做的参数打印信息
                    await self.press(char, {'delay': delay})
                else:
                    print("else char -->", char)  # 做的参数打印信息
                    await self.sendCharacter(char)
                if delay:
                    await asyncio.sleep(delay / 1000)
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3232 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 12:37 · PVG 20:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.