zoukankan      html  css  js  c++  java
  • java+selenium3-元素定位

    元素定位

    常见的元素定位方法

    (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()

    适用于任意页面元素,获取元素的tag name

    (10)getCssValue()

    适用于任意页面元素,获取当前页面元素的css属性信息

    (11)getLocation()

    适用于任意页面元素,获取元素在页面上的相对位置

    (12)getSize()

    适用于任意可见的页面元素,获取元素的宽度和高度信息

     参考资料:《基于Selenium 2的自动化测试》

  • 相关阅读:
    permission 文档 翻译 运行时权限
    TabLayout ViewPager Fragment 简介 案例 MD
    Log 日志工具类 保存到文件 MD
    OkHttp 官方wiki 翻译 MD
    Okhttp 简介 示例 MD
    OkHttp 官方Wiki之【使用案例】
    DialogPlus
    倒计时 总结 Timer Handler CountDownTimer RxJava MD
    RecyclerView 判断滑到底部 顶部 预加载 更多 分页 MD
    CSS3的媒体查询(Media Queries)与移动设备显示尺寸大全
  • 原文地址:https://www.cnblogs.com/marton/p/11255387.html
Copyright © 2011-2022 走看看