LoginSignup
0
1

More than 3 years have passed since last update.

Puppeteer 文字列で 特定の要素を選択したい時、またその文字列の近くの要素をクリックしたい時

Posted at

Puppeteer 文字列で 特定の要素を選択したい時、

またその文字列の近くの要素をクリックしたい時

なぜ文字列で選択したいか

・Xpath脳死コピペ
・HTMLタグ
・クラス名やID名
では限定しきれない時があるから

例えば
Yahoo!広告アカウントに送られてくる特定の名前のレポートのみをクリックしたい時
(あくまで抽象化するための例)

ポイントは2点

①文字列で選択する方法
②その文字列自体をクリック・抽出しても意味がない
aタグまでパスを渡す

結論:Xpathあんま理解してなかったのが俺が悪い

app.js
const reportPath =
    "//span[contains(text(), 'hogehoge')]/../..//a";

await page.waitForXPath(reportPath);
const reportClick = await page.$x(reportPath);
await reportClick[0].click();

``

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1