【node.js】seleniumの要素の特定方法一覧

seleniumでコーディング時、要素の特定の時のお作法などよく忘れるので、備忘録です。

スポンサーリンク

要素の特定方法

Xpath

await driver.findElements(By.xpath('//*[@id="Result"]/div[2]/p/span'))

クラス名

await driver.findElements(By.className('class'));

2つ以上のクラス名

await driver.findElements(By.css('.class1.class2'));
await driver.findElements(By.css('.class1.class2.class3'));

タグ名

await element.findElement(By.tagName('a'));
await element.findElement(By.tagName('h1'));

クラス名の中のaタグ

await driver.findElements(By.css('.ClassName > a'));

ボタンをクリックする

await driver.findElements(By.xpath('')).click();

戻り値をボタンにして、後ほどクリックする

const button = await driver.wait(until.elementLocated(By.className('class')), 5000);
await button.click();

テキストを取得する

.entry_title というclass名のタイトルを取得したい場合

let titleElement = await driver.wait(until.elementLocated(By.className('entry_title')), 5000);
let titleText = await titleElement.getText();

 

おすすめの記事