web自動化(2)----下拉框

方法1:將元素定義到父節點,再直接select

    Select select = new Select(element);

    select.selectByIndex(index);

    select.selectByValue(value);

    select.selectByVisibleText(text);

方法2:如果元素沒有被隱藏,可以直接找到需要的元素然後點擊

    WebElement selectele = driver.findElement(By.xpath(""));
    selectele.click();

	WebDriver driver = null;
	@Before
	public void before()
	{
		System.setProperty("webdriver.chrome.driver","C:\\Users\\Administrator\\Desktop\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions(); 
		options.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
		driver =  new ChromeDriver(options);
	}
	
	@Test
	public void select()
	{
		driver.get("http://brianreavis.github.io/selectize.js/");
		WebElement selectele = driver.findElement(By.id("select-beast"));
		/*
		 <select id="select-beast" class="demo-default" placeholder="Select a person...">
							<option value="">Select a person...</option>
							<option value="1">Chuck Testa</option>
							<option value="4">Sage Cattabriga-Alosa</option>
							<option value="3">Nikola Tesla</option>
						</select>
		*/
		Select se = new Select(selectele);
		se.selectByVisibleText("Sage Cattabriga-Alosa");
		/*
		 * <select id="select-gear" class="demo-default" placeholder="Select gear...">
							<option value="">Select gear...</option>
							<optgroup label="Climbing">
								<option value="pitons">Pitons</option>
								<option value="cams">Cams</option>
								<option value="nuts">Nuts</option>
								<option value="bolts">Bolts</option>
								<option value="stoppers">Stoppers</option>
								<option value="sling">Sling</option>
							</optgroup>
							<optgroup label="Skiing">
								<option value="skis">Skis</option>
								<option value="skins">Skins</option>
								<option value="poles">Poles</option>
							</optgroup>
						</select>
		*/
		WebElement selectele1 = driver.findElement(By.id("select-gear"));
		Select se1 = new Select(selectele1);
		se1.selectByValue("skins");
		/*
		 * <select id="select-gear" class="demo-default" placeholder="Select gear...">
							<option value="">Select gear...</option>
							<optgroup label="Climbing">
								<option value="pitons">Pitons</option>
								<option value="cams">Cams</option>
								<option value="nuts">Nuts</option>
								<option value="bolts">Bolts</option>
								<option value="stoppers">Stoppers</option>
								<option value="sling">Sling</option>
							</optgroup>
							<optgroup label="Skiing">
								<option value="skis">Skis</option>
								<option value="skins">Skins</option>
								<option value="poles">Poles</option>
							</optgroup>
						</select>
		*/
		WebElement selectele2 = driver.findElement(By.xpath("//*[@id=\"select-gear\"]/optgroup[2]/option[3]"));
		selectele2.click();
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章