最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【规避解决】pyppeteer不调试直接运行waitForSelector报错:ElementHandleError Evaluation failed TypeError MutationObserver is not a constructor at pollMutation

constructor crifan 439浏览 0评论
折腾:
【未解决】pyppeteer中page.querySelectorAll运行时无法获取到结果
期间,代码:
await page.waitForSelector('span.nums_text')
加调试,可以正常运行。
但是不调试直接运行报错:
发生异常: ElementHandleError
Evaluation failed: TypeError: MutationObserver is not a constructor
    at pollMutation (__pyppeteer_evaluation_script__:24:22)
    at waitForPredicatePageFunction (__pyppeteer_evaluation_script__:10:18)
  File "/Users/crifan/dev/dev_root/python/puppeteerBaiduSearch/puppeteerBaiduSearch.py", line 55, in main
    await page.waitForXPath("//span[@class='nums_text']")
  File "/Users/crifan/dev/dev_root/python/puppeteerBaiduSearch/puppeteerBaiduSearch.py", line 81, in <module>
    asyncio.get_event_loop().run_until_complete(main())
 
waitForSelector Evaluation failed: TypeError: MutationObserver is not a constructor
reactjs – react typescript testing TypeError: MutationObserver is not a constructor – Stack Overflow
node.js – React testing: “TypeError: MutationObserver is not a constructor” – Stack Overflow
reactjs – Mutation observer is not a constructor (Jest) – Stack Overflow
javascript – detect changes in DOM with puppeteer _ MutationObserver is working – Stack Overflow
await page.waitFor('span.nums_text')
错误依旧:
去看看动态函数提示:
没问题啊
waitForSelector ElementHandleError
javascript – 在Pyppeteer(Python Puppeteer)中从page.evaluate返回值到python – 堆栈内存溢出
https://stackoom.com/question/4TuBK/在Pyppeteer-Python-Puppeteer-中从page-evaluate返回值到python
venv/Lib/site-packages/tests/test_frame.py · master · zy / ReptileProject · GitLab
https://re.git.mygedit.com:444/zy/reptileproject/-/blob/master/venv/Lib/site-packages/tests/test_frame.py
pyppeteer 的基本使用 – 链滴
“* waitFor(): 等待页面上匹配的函数,超时或元素。参数可以是 int 或者 float,单位毫秒。也可以是 css 选择器”
models — pytest-pyppeteer latest documentation
            if _type == "css":
                await self.pyppeteer_page.waitForSelector(
                    locator_string, options=options
                )
            elif _type == "xpath":
                await self.pyppeteer_page.waitForXPath(locator_string, options=options)
python爬虫-pyppeteer常用API – 简书
  • select(): 选择选项并返回所选值。如果未找到元素,抛出 ElementHandleError。
  • waitFor(): 等待页面上匹配的函数,超时或元素。参数可以是 int 或者 float,单位毫秒。也可以是 CSS 选择器
  • waitForNavigation(): 等到网页跳转
  • waitForRequest(): 等待请求
  • waitForResponse(): 等待回应
  • waitForSelector(): 等到页面上出现匹配的元素。其实 waitFor()也有同样功能。
  • waitForFunction(): 等到函数完成并返回一个值
  • waitForXPath(): 等到 xpath 页面上出现匹配的元素
试试:
    await page.waitForXPath("//span[@class='nums_text']")
结果:错误一样:
-》感觉是:
python版本的puppeteer,内部的bug?
  • at pollMutation (__pyppeteer_evaluation_script__:24:22)
  • at waitForPredicatePageFunction (__pyppeteer_evaluation_script__:10:18)
__pyppeteer_evaluation_script__  MutationObserver is not a constructor
__pyppeteer_evaluation_script__ ElementHandleError
python – Pyppeteer: error when clicking on link with href “javascript:void(0);” – Stack Overflow
https://youtrack.jetbrains.com/issue/PY-33469
使用Pyppeteer抓取渲染网页 – 简书
或许需要启用js?
    # 是否启用JS,enabled设为False,则无渲染效果
    await page.setJavaScriptEnabled(enabled=True)
加上试试
    await page.setJavaScriptEnabled(enabled=True)
问题依旧。
另外看到:
使用Pyppeteer抓取渲染网页 – 简书
也是直接等待的。。。
    # 等待元素加载,第一种方法,强行等待5秒
    # await asyncio.sleep(5)
    
    # 第二种方法,在while循环里强行查询某元素进行等待
    while not await page.querySelector('.t'):
        pass
此处用:
await page.waitFor(2000) # millisecond
是可以:
确保百度触发搜搜后,等了2秒,页面内容加载了
后续即可找到元素了。
或者试试另外的:
    # Method 2: wait element showing
    while not await page.querySelector(SearchFoundWordsSelector):
        pass
优化后:
    # Method 2: wait element showing
    SingleWaitSeconds = 1
    while not await page.querySelector(SearchFoundWordsSelector):
      print("Still not found %s, wait %s seconds" % (SearchFoundWordsSelector, SingleWaitSeconds))
      await asyncio.sleep(SingleWaitSeconds)
      # pass
也是可以的:
【总结】
此处代码:
await page.waitForSelector('span.nums_text')
报错:
发生异常: ElementHandleError
Evaluation failed: TypeError: MutationObserver is not a constructor
后发现,类似代码:
    SearchFoundWordsSelector = 'span.nums_text'
    SearchFoundWordsXpath = "//span[@class='nums_text']"
    await page.waitForSelector(SearchFoundWordsSelector)
    await page.waitFor(SearchFoundWordsSelector)
    await page.waitForXPath(SearchFoundWordsXpath)
都会报同样错误
原因:估计是pyppeteer的bug
解决办法:无法解决。
暂时可以规避:
因为此处只是为了,检测页面,直到对应元素出现
所以可以改为其他办法检测:
办法1:直接wait等待
    # # Method 1: just wait
    await page.waitFor(2000) # millisecond
评价:不够好,不能精确判断元素是否出现
办法2:一直检测某元素,直到出现
    # Method 2: wait element showing
    SingleWaitSeconds = 1
    while not await page.querySelector(SearchFoundWordsSelector):
      print("Still not found %s, wait %s seconds" % (SearchFoundWordsSelector, SingleWaitSeconds))
      await asyncio.sleep(SingleWaitSeconds)
      # pass
评价:比较好,算是waitFor函数的等价版本

转载请注明:在路上 » 【规避解决】pyppeteer不调试直接运行waitForSelector报错:ElementHandleError Evaluation failed TypeError MutationObserver is not a constructor at pollMutation

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.137 seconds, using 20.56MB memory