找到下拉框所在元素
导入import org.openqa.selenium.support.ui.Select;
新建Select对象
通过getOptions拿到全部选项内容
通过selectByIndex或者selectByValue或者selectByVisibleText分别以index,value,text来选择
WebElement selectElement = driver.findElement(By.id("modules")); Select select = new Select(selectElement); List<WebElement> options = select.getOptions(); System.out.println(options); System.out.println(select.isMultiple()); select.selectByIndex(1); Thread.sleep(200); select.selectByValue("20"); Thread.sleep(2000); select.selectByVisibleText("switch");需要将driver的焦点切换到iframe中
页面上从0开始的iframe
driver.switchTo().frame(index);元素的id名称
driver.switchTo().frame(nameOrId);打开新页面之后需要进行切换才能定位到新页面的内容
切换需要传入窗口的handle句柄
driver.switchTo().window(nameOrHandle)完成一系列操作:
Actions actions = new Actions(driver); WebElement a = driver.findElement(By.id("a")); WebElement b = driver.findElement(By.id("b")); actions.clickAndHold(a).moveToElement(b).release().perform();