欢迎访问昆山宝鼎软件有限公司网站! 设为首页 | 网站地图 | XML | RSS订阅 | 宝鼎邮箱 | 宝鼎售后问题提交 | 后台管理


新闻资讯

MENU

软件开发知识

原文出处: 小坦克 本篇介绍我们如何利用selenium 来操作各种页面元素 链接(link) divp链接 link/

点击: 次  来源:宝鼎软件 时间:2017-07-28

原文出处: 小坦克

本篇先容我们如何操作selenium 来操纵各类页面元素

链接(link)

<div>
<p>链接 link</p>
<a href="www.cnblogs.com/tankxiao">小坦克</a>
</div>

链接的操纵

        // 找到链接元素
        WebElement link1 = driver.findElement(By.linkText("小坦克"));
        WebElement link11 = driver.findElement(By.partialLinkText("坦克"));
        
        // 点击链接
        link1.click();

输入框 textbox

   <div>
        <p>输入框 testbox</p>
        <input type="text" id="usernameid" value="username" />
    </div>

输入框的操纵

// 找到元素
        WebElement element = driver.findElement(By.id("usernameid"));
        
        // 在输入框中输入内容
        element.sendKeys("test111111");
        
        // 清空输入框
        element.clear();
        
        // 获取输入框的内容
        element.getAttribute("value");

按钮(Button)

    <div>
        <p>按钮 button</p>
        <input type="button" value="添加" id="proAddItem_0" />
    </div>

找到按钮元素

//找到按钮元素
        String xpath="//input[@value='添加']";
        WebElement addButton = driver.findElement(By.xpath(xpath));


        // 点击按钮
        addButton.click();


        // 判定按钮是否enable
        addButton.isEnabled();

下拉选择框(Select)

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

下拉选择框的操纵

// 找到元素
        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)

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

单选项元素的操纵

// 找到单选框元素
        String xpath="//input[@type='radio'][@value='Apple']";
        WebElement apple = driver.findElement(By.xpath(xpath));


        //选择某个单选框
        apple.click();


        //判定某个单选框是否已经被选择
        boolean isAppleSelect = apple.isSelected();


        // 获取元素属性
        apple.getAttribute("value");

多选框 check box

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

多选框的操纵和单选框一模一样的, 这里就不再讲了。

附: selenium java教程 (连载中, 敬请等候)

java selenium (一) selenium 先容

java selenium (二) 情况搭建要领一

java selenium (三) 情况搭建 基于Maven

java selenium (四) 利用欣赏器调试东西

java selenium (五) 元素定位大全

java selenium (六) xpath 定位

java selenium (七) CSS 定位

java selenium (八) Selenium IDE 用法

java selenium (九) 常见web UI 元素操纵 及API利用

java selenium (十) 操纵欣赏器

java selenium (十一) 操纵弹出对话框

java selenium (十二) 操纵弹出窗口

java selenium (十三) 智能期待页面加载完成

java selenium (十四) 处理惩罚Iframe 中的元素

图纸加密,软件开发