折腾:
【未解决】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
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
“* waitFor(): 等待页面上匹配的函数,超时或元素。参数可以是 int 或者 float,单位毫秒。也可以是 css 选择器”
if _type == "css": await self.pyppeteer_page.waitForSelector( locator_string, options=options ) elif _type == "xpath": await self.pyppeteer_page.waitForXPath(locator_string, options=options)
“
- 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
或许需要启用js?
# 是否启用JS,enabled设为False,则无渲染效果 await page.setJavaScriptEnabled(enabled=True)
加上试试
await page.setJavaScriptEnabled(enabled=True)
问题依旧。
另外看到:
也是直接等待的。。。
# 等待元素加载,第一种方法,强行等待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