LoginSignup
0
0

JavaScript の Selenium 使い方メモ (Node.js (Mocha))

Last updated at Posted at 2023-08-22

JavaScript の Selenium の使い方メモ
HTML の要素(DOM) 取得は一番使い勝手が良い By.css になれると良さそう
環境構築とかはこちらを参考にしてください

あと公式ドキュメントはこちら

実行の待機

await driver.sleep(1500) // 引数はミリ秒

ファイル操作

const fs = require('fs');

// 存在確認
fs.existsSync(filePath)
// ディレクトリ作成 (filePath 多重階層OK)
fs.mkdirSync(filePath, { recursive: true })
// ディレクトリ削除(filePath 直下にファイルあってもOK)
fs.rmdirSync(pdfPath, { recursive: true })
// ファイルコピー
fs.copyFileSync(oldFilePath, newFilePath)

スクリーンショット取得

// 全体
driver.takeScreenshot().then(function (image) {
    fs.writeFileSync(filePath, image, 'base64');
});

// 部分(タグ指定)
const element = await driver.findElement(By.css("#id"));
element.takeScreenshot().then(function (image) {
    fs.writeFileSync(filePath, image, 'base64');
});

URL入力

await driver.get(pageUrl)

ブラウザサイズ変更

await driver.manage().window().setRect({ width: 800, height: 600 })

テキスト入力, 要素クリック

await driver.findElement(By.css("#inputId")).sendKeys(value)
await driver.findElement(By.css("#clickId")).click()
0
0
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
0