第九篇: Java-Selenium之下拉框处理

程序1-练习处理下拉框:

  1. 打开浏览器
  2. 访问"http://toolsqa.com/automation-practice-form"
  3. 选择"Continents"下拉框(用id定位元素)
  4. 用selectByIndex方法选择Europe
  5. 用selectByVisibleText选择Africa
  6. 打印出所有下拉框的选项
  7. 关闭浏览器
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-练习处理多重选择框/列表:

  1. 打开浏览器
  2. 访问"http://toolsqa.com/automation-practice-form"
  3. 在多重选择框中选择’Selenium Commands’(使用name定位元素)
  4. 选择选项"Browser Commands"然后再取消选择 (使用selectByIndex和deselectByIndex)
  5. 选择选项"Navigation Commands"然后再取消选择(使用selectByVisibleText和deselectByVisibleText)
  6. 打印出并选择所有选项
  7. 取消选择所有选项
  8. 关闭浏览器
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();
	}

}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值