• 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的自动化测试》

  • 相关阅读:
    Java tomcat max-http-header-size配置导致的oom
    Idea修改jvm参数
    Java List的SubList使用问题
    Java Arrays.asList的三个坑
    Java 重写equals的时候为什么一定要重写hashcode-一个例子
    远心镜头
    镜头常识总结
    halcon中保存图像jpeg的压缩比
    红外光 相机拍照
    电磁波的穿透能力总结
  • 原文地址:https://www.cnblogs.com/marton/p/11255387.html
Copyright © 2020-2023  润新知