Selenium で web サーバ上のファイルの中身を取得する
10685 ワード
はじめに
Selenium を使用して, web サーバ上のファイルの中身を取得する方法です.
ダウンロードボタンを Selenium に押させて, ファイルを実際にダウンロードした後に中身を取得することも可能ですが,
その場合, ダウンロードしたファイル名を特定する必要があり, それが困難だったりします.
そこで, JavaScript の XMLHttpRequest() を使いファイルの中身1を取得する方法を, ケース毎のサンプルコードで紹介します.
ケース1: href の場合
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="https://localhost/files/hoge.csv">download</a>
</body>
</html>
main.php
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub');
$webDriver->get('https://localhost/index.html');
$url = $webDriver->findElement(WebDriverBy::tagName('a'))->getAttribute('href');
$responses = $webDriver->executeScript(implode('', ['var xhr = new XMLHttpRequest();',
'xhr.open("GET", "' . $url . '", false);',
'xhr.overrideMimeType("text/plain; charset=Shift_JIS");',
'xhr.send();',
'return xhr.responseText;']));
var_dump($responses);
$webDriver->quit();
ケース2: form method="get" の場合
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="https://localhost/files" method="get">
...
<input type="submit" value="download">
</form>
</body>
</html>
main.php
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub');
$webDriver->get('https://localhost/index.html');
$responses = $webDriver->executeScript(implode('', ['var form = document.querySelector("form");',
'var xhr = new XMLHttpRequest();',
'xhr.open("GET", form.action + "?" + new URLSearchParams(new FormData(form)).toString(), false);', // form.method => get, form.action => 'https://localhost/files/'
'xhr.overrideMimeType("text/plain; charset=Shift_JIS");',
'xhr.send();',
'return xhr.responseText;']));
var_dump($responses);
$webDriver->quit();
ケース3: form method="post" の場合
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="https://localhost/files" method="post">
...
<input type="submit" value="download">
</form>
</body>
</html>
main.php
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub');
$webDriver->get('https://localhost/index.html');
$responses = $webDriver->executeScript(implode('', ['var form = document.querySelector("form");',
'var xhr = new XMLHttpRequest();',
'xhr.open(form.method, form.action, false);', // form.method => post, form.action => 'https://localhost/files'
'xhr.overrideMimeType("text/plain; charset=Shift_JIS");',
'xhr.send(new FormData(form));',
'return xhr.responseText;']));
var_dump($responses);
$webDriver->quit();
注意事項
スクレイピングを禁止しているサイトもありますのでご利用の際はお気をつけくださいませ.
-
今回, web サーバ上のファイル = CSV ファイル を想定 ↩
Author And Source
この問題について(Selenium で web サーバ上のファイルの中身を取得する), 我々は、より多くの情報をここで見つけました https://qiita.com/y-kawano/items/9ded2d9e8e3c41b1ec1d著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .