要先選中這個彈出層的form元素,再找下拉框
public void downSelectBox(){driver.get("https://www.imooc.com/user/setprofile");driver.findElement(By.className("pull-right")).click();try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}//先獲取彈出層的form表單WebElement formElement = driver.findElement(By.id("profile"));WebElement job = formElement.findElement(By.id("job"));Select downList = new Select(job);downList.selectByIndex(2);//downList.selectByValue("1");//downList.selectByVisibleText("學生");downList.selectByIndex(3);//這個只能取消多選下拉框//downList.deselectByIndex(3);List<WebElement> list = downList.getAllSelectedOptions();for(WebElement option : list){System.out.println(option.getText());}System.out.println(downList.getFirstSelectedOption().getText());}
?