selenium实战:常见web UI 元素操作及API使用
package javaBase; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.Select; import java.util.List; /** * java selenium 常见web UI 元素操作 及API使用 */ public class TestSetSeleniumServerJAR { public static void main(String[] args) { //谷歌浏览器 WebDriver driver = new ChromeDriver(); //打开测试网址 driver.get("http://localhost:8888/Selenium/"); /** * 链接(link) */ /* // 找到链接元素 WebElement link1 = driver.findElement(By.linkText("itestor测试长流")); WebElement link11 = driver.findElement(By.partialLinkText("百度")); // 点击链接 //link1.click(); link11.click(); */ /** * 输入框 textbox */ /* // 找到元素 WebElement element = driver.findElement(By.id("usernameid")); //打印当前输入框的内容 System.out.println("当前输入框的内容是:" + element.getAttribute("value")); // 清空输入框 element.clear(); // 在输入框中输入内容 element.sendKeys("test111111"); // 获取输入框的内容 //element.getAttribute("value"); // 获取输入框的内容 String value1 = element.getAttribute("value"); //打印当前输入框内容 System.out.println("当前输入框的内容是:" + value1); // 清空输入框 element.clear(); //获取标题 String title = driver.getTitle(); //打印标题 System.out.printf("当前标题是:" +title); //关闭浏览器 //driver.close(); */ /** * 按钮(Button) */ /* //找到按钮元素 String xpath="//input[@value=‘添加‘]"; WebElement addButton = driver.findElement(By.xpath(xpath)); // 点击按钮 addButton.click(); // 判断按钮是否enable addButton.isEnabled(); */ /** * 下拉选择框(Select) */ /* // 找到元素 Select select = new Select(driver.findElement(By.id("proAddItem_kind"))); // 选择对应的选择项, index 从0开始的 select.selectByIndex(2); select.selectByValue("18"); select.selectByVisibleText("种类AA"); // 获取所有的选项 Listoptions = select.getOptions(); for (WebElement webElement : options) { System.out.println(webElement.getText()); } */ /** * 单选按钮(Radio Button) */ /* // 找到单选框元素 String xpath="//input[@type=‘radio‘][@value=‘Pear‘]"; WebElement Pear = driver.findElement(By.xpath(xpath)); //选择某个单选框 Pear.click(); //判断某个单选框是否已经被选择 boolean isAppleSelect = Pear.isSelected(); // 获取元素属性 Pear.getAttribute("value"); */ /** * 多选框 check box */ // 找到单选框元素 String xpath="//input[@type=‘checkbox‘][@value=‘Pear‘]"; WebElement Pear = driver.findElement(By.xpath(xpath)); //选择某个单选框 Pear.click(); //判断某个单选框是否已经被选择 boolean isAppleSelect = Pear.isSelected(); // 获取元素属性 Pear.getAttribute("value"); } }
测试 html源码
Selenium Test 输入框 testbox
按钮 button
下拉选择框框 Select
单选项 Radio Button
Apple Pear Banana Orange多选项 checkbox
Apple Pear Banana Orange
原文:https://www.cnblogs.com/itestor/p/15233298.html