元素定位
常见的元素定位方法
(1)通过ID查找元素
(2)通过Name查找元素
(3)通过ClassName查找元素
(4)通过TagName查找元素
(5)通过LinkText查找元素
(6)通过PartialLinkText查找元素
(7)通过CSS选择器查找元素
(8)通过XPath查找元素
(9)通过jQuery查找元素
代码示例:
1 package com.java.sele; 2 3 import java.util.List; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.WebElement; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 11 public class Test { 12 public static void main(String[] args) { 13 WebDriver driver; 14 //设置浏览器驱动环境变量 15 System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\ChromeCore\\chromedriver.exe"); 16 driver = new ChromeDriver(); 17 driver.get("D:\\Temp\\selenium_locate_ele.html"); 18 19 //1.id定位 20 WebElement idEle = driver.findElement(By.id("id_value")); 21 idEle.click(); 22 idEle.sendKeys("id定位"); 23 24 //2.name定位 25 WebElement nameEle = driver.findElement(By.name("search_input2")); 26 nameEle.click(); 27 nameEle.sendKeys("name定位"); 28 29 //3.class定位 30 WebElement classEle = driver.findElement(By.className("search_input_Box")); 31 classEle.click(); 32 classEle.sendKeys("class"); 33 34 //4.TagName定位 35 List<WebElement> inputEles = driver.findElements(By.tagName("input")); 36 inputEles.get(0).click(); 37 38 //5.LinkText定位 39 WebElement linkTextEle = driver.findElement(By.linkText("联系方式")); 40 41 //6.ParticalLinkText定位 42 WebElement PartLinkTextEle = driver.findElement(By.partialLinkText("联系")); 43 44 //7.css选择器定位 45 WebElement cssSelEle = driver.findElement(By.cssSelector("#st-lib")); 46 47 //8.xpath定位 48 WebElement xpathEle = driver.findElement(By.xpath("/html/body/div[5]/div[1]/ul/li[10]/a")); 49 } 50 }
常见元素的Actions
Actions简介:
(1)sendKeys()
适用于具有文本编辑区域的页面元素,常用于文本框输入字符串
(2)clear()
适用于具有文本编辑区域的页面元素,清除输入的文本信息
(3)submit()
将form表单提交到web服务器
(4)isdisplayed()
(5)isEnabled()
(6)isSelected()
多用于单选框和多选框,判断元素是否被选中
(7)getAttribute()
(8)getText()
(9)getTagName()
(10)getCssValue()
(11)getLocation()
(12)getSize()
参考资料:《基于Selenium 2的自动化测试》
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。