zoukankan      html  css  js  c++  java
  • Selenium_WebDriver_元素方法

    版权声明:本文为博主原创文章,转载请注明出处。


       前面已经学习了定位元素,定位只是第一步,定位之后需要对这个元素进行操作,如在百度搜索首页的输入框进行输入文本,对"百度一下"按钮进行单击等,下面就来对这些最常用的方法做一个总结。

    方法

    描述

    用法示例

    void clear()

    清空“文本输入元素”的值;

    对其他元素没有影响。

    示例:清除百度搜索输入栏输入的文本;

    方法:driver.findElement(By.id("kw")).clear();

    void click()

    单击此元素。

    示例:点击百度搜索主页的“新闻”文本链接;

    方法:driver.findElement(By.linkText("新闻")).click();

    WebElement findElement(By by)

    使用给定的方法找到的第一个元素;

    示例:定位百度搜索主页的搜索输入框;

    方法:driver.findElement(By.id("kw"));

    java.util.List<WebElement

    findElements(By by)

    使用给定的机制查找当前上下文中的所有元素;

    示例:查找所有的 input 元素;

    方法:List<WebElement> inputs = driver.findElements(By.tagName("input"));

    备注:可借助List的相关方法进行进一步定位、操作;

    java.lang.String 

    getAttribute(java.lang.String name)

    获得给定的元素某一属性的值;

    示例:获得“百度搜索输入框”type属性的值;

    方法:System.out.println("输入框type属性:"+

    driver.findElement(By.id("kw")).getAttribute("type"));

    java.lang.String 

    getCssValue(java.lang.String propertyName)

    获取给定css属性的值;

    CSS属性(如背景、字体、边框等)可按照DOM CSS2规范获取;

    示例:获得“百度一下按钮”的颜色值;

    方法:System.out.println("百度一下按钮颜色:"+

    driver.findElement(By.id("su")).getCssValue("backgroundColor"));

    Point getLocation()

    返回该元素相对页面左上角的位置坐标

    示例:返回“百度一下按钮”相对页面左上角的位置坐标

    方法:System.out.println("Point:"+

    driver.findElement(By.id("su")).getLocation());

    Rectangle getRect()

    获取渲染元素的位置和大小;

    Chrome & IE可能不支持;

    ——

    Dimension getSize()

    获取渲染元素的宽度和高度;

    示例:获取百度搜索框的宽度和高度

    方法:System.out.println("搜索框:"+

    driver.findElement(By.id("kw")).getSize());

    java.lang.String getTagName()

    获取该元素的标记名称;

    示例:

    方法:System.out.println("更多产品的tagname:"+

    driver.findElement(By.linkText("更多产品")).getTagName());  

    场景:<a href="http://www.baidu.com/more/" name="tj_briicon" class="s_bri" target="_blank"> 更多产品</a>

    java.lang.String getText()

    获取元素的文本。

    示例:获取百度首页底部的备案显示;

    方法:System.out.println("Text:"+

    driver.findElement(By.id("cp")).getText());

    boolean isDisplayed()

    判断当前元素是否显示;

    True=显示,False=隐藏;

    示例:判断百度首页底部的备案是否显示;

    方法:System.out.println("Text:"+

    driver.findElement(By.id("cp")).isDisplayed());

    boolean isEnabled()

    判断元素是否可用;

    ——

    boolean isSelected()

    确定该元素是否被选中;

    此操作只适用于输入元素,如复选框、选择和单选按钮中的选项。

    ——

    void sendKeys

    (

    java.lang.CharSequence... keysToSend

    )

    模拟输入一个元素;

    示例:在百度搜索框中输入“Selenium;

    方法:driver.findElement(By.id("kw")).sendKeys("Selenium");

    void submit()

    提交表单给远程服务器;

    注:如果提交导致当前页面更改,则此方法将阻塞,直到加载到新页为止。

    异常:NoSuchElementException——如果给定元素不在表单内

    示例:在百度搜索框中输入“Selenium”,并发送确定搜索请求到服务器;

    方法:driver.findElement(By.id("su")).submit();

    以上测试均以 " driver.get("https://www.baidu.com/");  " 为前提;

      测试代码如下:

    package com.SeleniumLib.jase;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Point;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class WebElementOperate {
        public static void main(String[]args){
            System.out.println("start selenium");
    
            WebDriver   driver;    
            System.setProperty("webdriver.chrome.driver","D:/selenium-java-3.5.3/chromedriver.exe");   //chromedriver驱动的本地存放路径   
            driver = new ChromeDriver(); 
            driver.get("http://www.baidu.com/"); 
            /*driver.findElement(By.id("kw")).sendKeys("Selenium");
            driver.findElement(By.id("su")).click();*/
            
            //清空文本输入元素的值
            /*driver.findElement(By.id("kw")).sendKeys("Selenium");
            driver.findElement(By.id("kw")).clear();*/
            
            //定位元素   && 单击此元素
            /*driver.findElement(By.linkText("新闻")).click();*/
            
            //查找所有tagname=input的所有元素
            /* List<WebElement> inputs = driver.findElements(By.tagName("input"));  //import java.util.List;
            System.out.println(inputs.size()); */
            
            //获得“百度搜索输入框”type属性的值;
            /*System.out.println("百度搜索输入框type属性:"+driver.findElement(By.id("kw")).getAttribute("type"));*/
        
            //获取给定css属性的值
            //System.out.println("borderBottomColor:"+driver.findElement(By.id("su")).getCssValue("backgroundColor"));  //获得“百度一下按钮”的颜色值;
            //System.out.println("color:"+driver.findElement(By.id("su")).getCssValue("color"));  //获得“百度一下字体”的颜色值;
           
            //返回“百度一下按钮”相对页面左上角的位置坐标
            //System.out.println("Point:"+driver.findElement(By.id("su")).getLocation());  //import org.openqa.selenium.Point;
    
            //获取渲染元素的位置和大小
            //----Chrome下测试为ERROR:System.out.println("Rect:"+driver.findElement(By.id("kw")).getRect());
            
            //获取渲染元素的宽度和高度;
            //System.out.println("搜索框:"+driver.findElement(By.id("kw")).getSize());
            //System.out.println("百度一下按钮:"+driver.findElement(By.id("su")).getSize());
           
            //获取元素的标记名称;
            //System.out.println("kw的tagname:"+driver.findElement(By.id("kw")).getTagName());
            //System.out.println("更多产品的tagname:"+driver.findElement(By.linkText("更多产品")).getTagName());  //场景:<a href="http://www.baidu.com/more/" name="tj_briicon" class="s_bri" target="_blank"> 更多产品</a>
        
            //获取文本元素的文本信息;
            //System.out.println("Text:"+driver.findElement(By.id("cp")).getText());  //百度底部备案信息
            
            //判断当前元素是否显示
            //System.out.println("Text:"+driver.findElement(By.id("cp")).isDisplayed());  //True //看到有人说:这个函数用于判断某个元素是否存在页面上,这里的存在不是肉眼看到的存在,而是html代码的存在。某些情况元素的visibility为hidden或者display属性为none,我们在页面看不到但是实际是存在页面的一些元素;目前未证实该说法;
            
            //在百度搜索框中输入“Selenium”
            //driver.findElement(By.id("kw")).sendKeys("Selenium");
            
            //在百度搜索框中输入“Selenium”,并发送确定搜索请求到服务器
            driver.findElement(By.id("kw")).sendKeys("Selenium");
            driver.findElement(By.id("su")).submit();
            
        }
    }

      CSS属性可参考:http://www.w3school.com.cn/jsref/dom_obj_style.asp#background

      Selenium Java API参考路径:https://seleniumhq.github.io/selenium/docs/api/java/index.html

  • 相关阅读:
    Python统计后台数据
    jemter上传到服务器进行压测
    Python的文件读写
    Git中的Stash Changes和UnStash Changes用法
    算法集锦
    各种排序介绍
    SQLServerDAL的引用找不到
    抽象是什么?
    自己写的js分页
    动手写代码
  • 原文地址:https://www.cnblogs.com/xpp142857/p/7551565.html
Copyright © 2011-2022 走看看