본문 바로가기

테스트툴/Selenium

Selenium Webdriver 드롭다운에서 값 선택

실습은

 

Register: Mercury Tours

 

demo.guru99.com

위 페이지를 활용했다

 

페이지 소스에서 

드롭다운 박스의 name을 확인

Selenium의 Select 패키지를 활용하여 Country의 옵션 중 CANADA를 선택하도록 한다

package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;//webElement 사용
import org.openqa.selenium.support.ui.Select;//Select

public class MyClass {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		String getURL = "http://demo.guru99.com/test/newtours/register.php";
		driver.get(getURL);
		
		Select drpCountry = new Select (driver.findElement(By.name("country")));
		drpCountry.selectByVisibleText("CANADA");
	}
}

 

결과


다중 Select 요소에서 여러 옵션을 선택할 때 selectByVisibleText() 메소드를 사용할 수 있다

 

실습은

 

Multiple Select Sample

 

output.jsbin.com

위 페이지를 활용한다

select의 id를 확인한다

select에서 Banana와 Grape를 선택하였다가 모두 해제하도록 한다

package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;//webElement 사용
import org.openqa.selenium.support.ui.Select;

public class MyClass {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		//String getURL = "";
		String getURL = "http://jsbin.com/osebed/2";
		driver.get(getURL);
		
		Select fruits = new Select(driver.findElement(By.id("fruits")));
		fruits.selectByVisibleText("Banana");
		fruits.selectByIndex(3);
		fruits.deselectAll();
	}
}

fruits.selectByVisibleText("Banana"); 바나나라는 txt를 찾아 선택

fruits.selectByIndex(3); 인덱스 3번을 찾아 선택

fruits.deselectAll(); 모두 선택 해제