Selenium で web サーバ上のファイルの中身を取得する


はじめに

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();

注意事項

スクレイピングを禁止しているサイトもありますのでご利用の際はお気をつけくださいませ.


  1. 今回, web サーバ上のファイル = CSV ファイル を想定