トッカンソフトウェア

Selenium By




属性指定

html



<!DOCTYPE html>
<html>

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

<body>
	<input id="id1" name="name1" class="class1" value="aaaa" />
	<a href="Javascript:window.alert('hello')">リンクテキスト</a>
	<div>abcd</div>
</body>

</html>


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("txt1"));
//		WebElement element = driver.findElement(By.name("name1"));
//		WebElement element = driver.findElement(By.className("class1"));
//		WebElement element = driver.findElement(By.xpath("//*[@value='aaaa']"));
//		WebElement element = driver.findElement(By.tagName("input"));

		element.clear();
		element.sendKeys("Hello World");

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

属性指定はBy.*で指定します。細かく指定する場合、xpathで指定します。

xpath

html


<!DOCTYPE html>
<html>

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

<body>
	<input id="txt1" value="aaaa" />
	<div>abcd</div>
</body>

</html>

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.xpath("/html/body/input[@id='txt1']"));
		// WebElement element = driver.findElement(By.xpath("//input[@id='txt1']"));
		// WebElement element = driver.findElement(By.xpath("//*[@id='txt1']"));
		// WebElement element = driver.findElement(By.xpath("//*[contains(@value, 'aa')]"));
	
		element.clear();
		element.sendKeys("Hello World");

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

/html/body/inputのようにタグの位置を指定できます。//のように省略することもできます。
属性には@を付けます。

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.xpath("//div[contains(text(), 'bc')]"));

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

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

タグの中の文字列を条件にする場合、text()を使用します。

結果


abcd

その他の条件


//div[not(contains(text(), 'bc'))]/@src
//div[contains(text(), 'abc') or contains(text(), 'def')]
//div[contains(text(), 'abc') and contains(text(), 'def')]


xpathで前後を取得

html


<!DOCTYPE html>
<html>

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

<body>
	<table>
		<tr>
			<td>abc</td><td>def</td><td>ghi</td>
		</tr>
	</table>
</body>

</html>

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 pos1 = driver.findElement(By.xpath("//td[1]"));
		WebElement pos2 = driver.findElement(By.xpath("//td[position()=3]"));

		System.out.println(pos1.getText());
		System.out.println(pos2.getText());

		WebElement eleBefore = driver.findElement(By.xpath("//td[contains(text(), 'de')]/preceding-sibling::td[1]"));
		WebElement eleAfter = driver.findElement(By.xpath("//td[contains(text(), 'de')]/following-sibling::td[1]"));

		System.out.println(eleBefore.getText());
		System.out.println(eleAfter.getText());

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

結果


abc
ghi
abc
ghi

位置を指定して要素を取得する場合は、[数値]または[position()=数値]で指定します。
指定要素の前後の要素を取得する場合は、preceding-sibling、following-siblingを指定します。

複数取得

java



import java.util.List;

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

		List<WebElement> elements = driver.findElements(By.tagName("td"));

		for (WebElement element : elements) {
			System.out.println(element.getText());
		}

		Thread.sleep(1000 * 10);

		driver.quit();
	}
}

abc
def
ghi

条件に合う要素を複数取得する場合は、findElementsを使用します。

ページのトップへ戻る