WebDriver 简单使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36511401/article/details/102558913

1、输入框(text field or textarea)
    WebElement we = driver.findElement(By.id("id"));
    we.clear(); //将输入框清空
    we.sendKeys(“test”);// 在输入框中输入内容
    element.getAttribute("value"); // 获取输入框的文本内容, 取得就是 value 属性的值 


2、 下拉选择框(select)
    Select select = new Select(driver.findElement(By.id("id"))); // 找到下拉选择框的元素
    select.selectByVisibleText(“ 北京市 ”); // 通过可见文本去选择
    select.selectByValue(“beijing”); // 通过 html 标签中的 value 属性值去选择
    select.selectByIndex(1); // 通过 index(索引从0开始)选择
    select.deselectAll(); 
    select.deselectByValue(“ 替换成实际的值 ”); 
    select.deselectByVisibleText(“ 替换成实际的值 ”); 
    List<WebElement> wes = select.getAllSelectedOptions();// 获取所有选择项的值
    String text = select.getFirstSelectedOption().getText();// 获取第一个选择项或者默认选择项

3、 单选框(Radio Button)
    WebElement we =driver.findElement(By.id("id")); // 找到单选框元素
    we.click(); // 选择某个单选项
    we.clear(); // 清空某个单选项 
    we.isSelected();// 判断某个单选项是否已经被选择, 返回的是 Boolean 类型

4、多选框(Checkbox)
    WebElement checkbox = driver.findElement(By.id("id")); // 找到多选框元素
    checkbox.click();// 点击复选框 
    checkbox.clear();// 清除复选
    checkbox.isSelected();// 判断复选框是否被选中
    checkbox.isEnabled(); // 判断复选框是否可用

5、按钮(Button)
    WebElement saveButton = driver.findElement(By.id("id")); // 找到按钮元素 
    saveButton.click(); // 点击按钮 
    saveButton.isEnabled ();// 判断按钮是否可用 

6、左右选择框
    // 左边是可供选择项,选择后移动到右边的框中,反之亦然,先处理选择框
    Select lang = new Select(driver.findElement(By.id("languages"))); 
    lang.selectByVisibleText(“English”);
    // 再处理向右移动的按钮
    WebElement addLanguage = driver.findElement(By.id("addButton")); 
    addLanguage.click();

7、弹出对话框(Popup dialogs)
    Alert alert = driver.switchTo().alert();// 切换到弹出框 
    alert.accept();// 确定
    alert.dismiss();// 取消或者点"X"
    alert.getText();// 获取弹出框文本内容 

8、表单(Form)
    driver.findElement(By.id("approve")).submit();// 只适合表单的提交

9、上传文件 (Upload File)
    WebElement adFileUpload = driver.findElement(By.id("id"));// 定位上传控件
    String filePath = "C:\\test\\uploadfile\\test.jpg";// 定义了一个本地文件的路径 
    adFileUpload.sendKeys(filePath);// 为上传控件进行赋值操作,将需要上传的文件的路径赋给控件

猜你喜欢

转载自blog.csdn.net/qq_36511401/article/details/102558913