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"); // 获取所有的选项 List<WebElement> options = 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源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Selenium Test</title>
</head>
<body>
<div>
<p>博客园 测试长流</p>
<a href="https://www.cnblogs.com/itestor/">itestor测试长流</a>
<p>百度 首页</p>
<a href="https://www.baidu.com/">itestor百度一下</a>
</div>
<div>
<p>输入框 testbox</p>
<input type="text" id="usernameid" value="username123" />
</div>
<div>
<p>按钮 button</p>
<input type="button" value="添加" id="proAddItem_0" />
</div>
<div>
<p>下拉选择框框 Select</p>
<select id="proAddItem_kind" name="kind">
<option value="1">电脑硬件</option>
<option value="2">房产</option>
<option value="18">种类AA</option>
<option value="19">种类BB</option>
<option value="20">种类BB</option>
<option value="21">种类CC</option>
</select>
</div>
<div>
<p>单选项 Radio Button</p>
<input type="radio" value="Apple" name="fruit>" />Apple
<input type="radio" value="Pear" name="fruit>" />Pear
<input type="radio" value="Banana" name="fruit>" />Banana
<input type="radio" value="Orange" name="fruit>" />Orange
</div>
<div>
<p>多选项 checkbox</p>
<input type="checkbox" value="Apple" name="fruit>" />Apple
<input type="checkbox" value="Pear" name="fruit>" />Pear
<input type="checkbox" value="Banana" name="fruit>" />Banana
<input type="checkbox" value="Orange" name="fruit>" />Orange
</div>
</body>
</html>
selenium实战:常见web UI 元素操作及API使用
原文:https://www.cnblogs.com/itestor/p/15233298.html