Selenium 入力項目
ボタン
html
<input id="btn1" type="button" value="ボタン1" onclick="alert('ボタン1')"/>
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");
driver.findElement(By.id("btn1")).click();
Thread.sleep(1000 * 10);
driver.quit();
}
}
テキスト
html
<input id="txt1" type="text" value="テキスト1" />
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("txt1"));
element.clear();
element.sendKeys("こんにちは、世界");
System.out.println(element.getAttribute("value"));
Thread.sleep(1000 * 10);
driver.quit();
}
}
sendKeysで文字列入力はできますが、前の入力値が残るのでclearを呼び出しています。
セレクト(コンボボックス)
html
<select id="sel1">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
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;
import org.openqa.selenium.support.ui.Select;
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("sel1"));
Select select = new Select(element);
select.selectByIndex(0);
show("1番目を選択", select);
select.selectByValue("2");
show("2番目を選択", select);
select.selectByVisibleText("c");
show("3番目を選択", select);
WebElement next = nextOption(driver, By.id("sel1"));
select.selectByValue(next.getAttribute("value"));
show("次を選択", select);
Thread.sleep(1000 * 10);
driver.quit();
}
private static void show(String str, Select select) {
List<WebElement> options = select.getAllSelectedOptions();
for (WebElement option : options) {
System.out.println(str + ":" + option.getAttribute("value") + ":" + option.getText());
}
}
private static WebElement nextOption(WebDriver driver, By by) {
WebElement element = driver.findElement(by);
Select select = new Select(element);
List<WebElement> options = select.getOptions();
List<WebElement> selects = select.getAllSelectedOptions();
if (selects.size() == 0) {
return options.get(0);
}
String target = selects.get(0).getAttribute("value");
boolean isFind = false;
for (WebElement option : options) {
if (isFind) {
return option;
}
if (target.equals(option.getAttribute("value"))) {
isFind = true;
}
}
return options.get(0);
}
}
結果
1番目を選択:1:a
2番目を選択:2:b
3番目を選択:3:c
次を選択:1:a
セレクト(リストボックス)
html
<select id="sel2" multiple>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
</select>
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;
import org.openqa.selenium.support.ui.Select;
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("sel2"));
Select select = new Select(element);
select.deselectAll();
select.selectByIndex(0);
select.selectByValue("2");
select.selectByVisibleText("c");
show("1、2、3番目を選択", select);
select.deselectByIndex(0);
select.deselectByValue("2");
select.deselectByVisibleText("c");
show("選択解除", select);
Thread.sleep(1000 * 10);
driver.quit();
}
private static void show(String str, Select select) {
List<WebElement> options = select.getAllSelectedOptions();
for (WebElement option : options) {
System.out.println(str + ":" + option.getAttribute("value") + ":" + option.getText());
}
if (options.size() == 0) {
System.out.println("選択なし");
}
}
}
結果
1、2、3番目を選択:1:a
1、2、3番目を選択:2:b
1、2、3番目を選択:3:c
選択なし
selectByIndexなどはセレクト(コンボボックス)と同様です。ここでは選択を外すdeselectByIndexなどを呼び出しています。
チェックボックス
html
<input type="checkbox" name="chk1" value="a" />あああ<br>
<input type="checkbox" name="chk1" value="b" checked />いいい<br>
<input type="checkbox" name="chk1" value="c" />ううう<br>
<br />
<input type="checkbox" id="chkID" value="d" />単独チェックボックス<br>
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");
WebElement element = driver.findElement(By.id("chkID"));
if (element.isSelected()) {
element.click();
}
List<WebElement> chks = driver.findElements(By.name("chk1"));
for (WebElement chk : chks) {
if (chk.isSelected()) {
System.out.println(chk.getAttribute("value"));
}
}
Thread.sleep(1000 * 10);
driver.quit();
}
}
isSelectedでチェックボックスがチェックされていたら、clickでチェックを外すということをやっています。
ラジオボタン
html
<input type="radio" name="chk1" value="a" />あああ<br>
<input type="radio" name="chk1" value="b" checked />いいい<br>
<input type="radio" name="chk1" value="c" />ううう<br>
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");
WebElement element = driver.findElement(By.xpath("//input[@value='a' and @name='chk1']"));
element.click();
List<WebElement> chks = driver.findElements(By.name("chk1"));
for (WebElement chk : chks) {
if (chk.isSelected()) {
System.out.println(chk.getAttribute("value"));
}
}
Thread.sleep(1000 * 10);
driver.quit();
}
}
ページのトップへ戻る