トッカンソフトウェア

Selenium 画面操作




テキスト取得

html


<div id="dv1">
	あ<br />いう<span>え</span>お
</div>

java


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

	public static void main(String[] arg) throws InterruptedException {

		WebDriver driver = new ChromeDriver();

		driver.get("http://localhost/test/test.html");

		WebElement element = driver.findElement(By.id("dv1"));

		System.out.println(element.getText());

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

結果


あ
いうえお


ブラウザ戻る進むリロード

html


<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>テストページ</title>
</head>

<body>
	<input id="btn1" type="button" value="ボタン1" onclick="location.href='test.html?aaa'" />
</body>

</html>

java


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

	public static void main(String[] arg) throws InterruptedException {

		WebDriver driver = new ChromeDriver();

		driver.get("http://localhost/test/test.html");

		driver.findElement(By.id("btn1")).click();

		Thread.sleep(1000 * 2);
		// 戻る
		driver.navigate().back();
		Thread.sleep(1000 * 2);
		// 進む
		driver.navigate().forward();
		Thread.sleep(1000 * 2);
		// リロード
		driver.navigate().refresh();
		Thread.sleep(1000 * 10);

		driver.quit();
	}
}


スクリーンショット

java


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

	public static void main(String[] arg) throws InterruptedException {

		WebDriver driver = new ChromeDriver();

		driver.get("http://localhost/test/test.html");

		File tmpFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

		try {

			Files.copy(tmpFile.toPath(), Paths.get("C:\\work", tmpFile.getName()), StandardCopyOption.REPLACE_EXISTING);

		} catch (IOException e) {

			e.printStackTrace();
		}

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}


ダウンロード

java


import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

	public static void main(String[] arg) throws InterruptedException {

		WebDriver driver = new ChromeDriver();

		driver.get("http://localhost/test/test.html");

		long beforeStart = System.currentTimeMillis() - 1000;

		driver.findElement(By.linkText("download")).click();

		File downFile = getDownloadEnd(beforeStart);

		if (downFile != null) {
			System.out.println(downFile.getName());
		}

		Thread.sleep(1000 * 10);

		driver.quit();

	}

	private static File getDownloadEnd(long beforeStart) throws InterruptedException {

		File downFile = getDownloadStart(beforeStart);

		if (downFile == null) {

			return null;
		}

		for (int i = 0; i < 30; i++) {

			if (!downFile.exists()) {

				downFile = getDownloadStart(beforeStart);
			}

			if (downFile.canWrite()) {

				return downFile;
			}
			Thread.sleep(1000);
		}
		return null;
	}

	private static File getDownloadStart(long beforeStart) throws InterruptedException {

		String downPath = System.getProperty("user.home") + "\\Downloads\\";

		File downDir = new File(downPath);

		for (int i = 0; i < 30; i++) {

			File[] downFiles = downDir.listFiles();

			for (File downFile : downFiles) {

				if (!downFile.isFile()) {

					continue;
				}

				if (downFile.getName().endsWith(".tmp")) {

					continue;
				}

				if (downFile.lastModified() > beforeStart) {

					return downFile;
				}

			}
			Thread.sleep(1000);
		}

		return null;
	}
}


その他

java


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

	public static void main(String[] arg) throws InterruptedException {

		WebDriver driver = new ChromeDriver();

		driver.get("http://localhost/test/test.html");

		// ウィンドウ最大化
		driver.manage().window().maximize();

		// ソースの取得
		System.out.println(driver.getPageSource());

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}




ページのトップへ戻る