程序1-练习处理下拉框:
- 打开浏览器
- 访问"http://toolsqa.com/automation-practice-form"
- 选择"Continents"下拉框(用id定位元素)
- 用selectByIndex方法选择Europe
- 用selectByVisibleText选择Africa
- 打印出所有下拉框的选项
- 关闭浏览器
package automationFramework;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class DropDownMultipleSelect {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "F:\\\\workspace\\ToolsQA\\OnlineStore\\geckodriver-v0.26.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String bdURL = "http://toolsqa.com/automation-practice-form";
driver.navigate().to(bdURL);
//选择"Continents"下拉框(用id定位元素)
//注意:Select class only works for elements with <select> tags
Select oSelect = new Select(driver.findElement(By.id("continents")));
//用selectByIndex方法选择Europe
oSelect.selectByIndex(1);
//用selectByVisibleText选择Africa
oSelect.selectByVisibleText("Africa");
Thread.sleep(2000);
//打印出所有下拉框的选项
List<WebElement> osize = oSelect.getOptions();
for(int i=0;i<osize.size();i++) {
String sValue=oSelect.getOptions().get(i).getText();
System.out.println(sValue);
if(sValue.equalsIgnoreCase("South America")) {
oSelect.selectByIndex(i);
break;
}
}
driver.close();
}
}
程序2-练习处理多重选择框/列表:
- 打开浏览器
- 访问"http://toolsqa.com/automation-practice-form"
- 在多重选择框中选择’Selenium Commands’(使用name定位元素)
- 选择选项"Browser Commands"然后再取消选择 (使用selectByIndex和deselectByIndex)
- 选择选项"Navigation Commands"然后再取消选择(使用selectByVisibleText和deselectByVisibleText)
- 打印出并选择所有选项
- 取消选择所有选项
- 关闭浏览器
package automationFramework;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class MultipleSelect {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "F:\\\\workspace\\ToolsQA\\OnlineStore\\geckodriver-v0.26.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String bdURL = "http://toolsqa.com/automation-practice-form";
driver.navigate().to(bdURL);
// 在多重选择框中选择'Selenium Commands'(使用name定位元素)
Select oSelect = new Select(driver.findElement(By.name("selenium_commands")));
//选择选项"Browser Commands"然后再取消选择 (使用selectByIndex和deselectByIndex)
oSelect.selectByIndex(0);
Thread.sleep(5000);
oSelect.deselectByIndex(0);
//选择选项"Navigation Commands"然后再取消选择(使用selectByVisibleText和deselectByVisibleText)
oSelect.selectByVisibleText("Navigation Commands");
Thread.sleep(5000);
oSelect.deselectByVisibleText("Navigation Commands");
//打印出并选择所有选项
List<WebElement> oSize = oSelect.getOptions();
for(int i=0;i<oSize.size();i++) {
System.out.println(oSelect.getOptions().get(i).getText());
oSelect.getOptions().get(i).click();
}
//取消选择所有选项
oSelect.deselectAll();
//关闭浏览器
Thread.sleep(5000);
driver.close();
}
}