在 Pyppeteer (Python Puppeteer) 中通过文本选择按钮 [英] select a button by its text in Pyppeteer (Python Puppeteer)

查看:425
本文介绍了在 Pyppeteer (Python Puppeteer) 中通过文本选择按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pyppeteer

<button class="r3f3s" tabindex="0">Text here</button>

我正在尝试使用 Jquery 来做到这一点,就像建议的 这里:

I am trying to do it using Jquery like suggested here:

btn = await page.querySelector('button[text=\'Text here\']')

或在这里:

btn = await page.querySelector('button:contains(text(), 'Text here'))

但是好像不行.我还注意到很多在 Jquery 中在线发布的示例似乎不适用于 Google chrome 的控制台,因此不适用于 pyppeteer.

But it doesn't seem to work. I also noticed that a lot of examples published online in Jquery doesn't seem to work it Google chrome's console, thus doesn't work on pyppeteer.

我的问题是如何通过文本选择按钮,而且,任何人都可以指导我找到一个指南,解释什么 Jquery 选择器语法适合在 Google chrome 的控制台 (puppeteer) 上工作?因为 这个 似乎不正确,这个错误并不是真正的指示性:

My question is how can I select button by its text, and also, can anyone direct me to a guide explaining what Jquery selector syntax is the right one to work on Google chrome's console (puppeteer)? because this one doesn't seem correct and this error is not really indicative:

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': is not a valid selector.
at <anonymous>:1:10

推荐答案

CSS 选择器包含当前不支持的文本,但您可以使用 Xpath

CSS selector contain text currently not supported but you can use Xpath

完全匹配文本

btn = await page.$x('//button[text()="Text here"]')

包含文本

btn = await page.$x('//button[contains(text(), "Text he")]')

演示:

这篇关于在 Pyppeteer (Python Puppeteer) 中通过文本选择按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆