zoukankan      html  css  js  c++  java
  • java之web自动化,下拉框操作,日期控件,拖拽

    下拉框处理类:Select

    如果页面元素是个下拉框,我们可以将此web元素封装为Select对象。

    package com.web_java01;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Select;
    import org.testng.annotations.Test;
    
    import java.util.List;
    
    public class web_test06 extends Base{
        @Test
        public void test() throws InterruptedException {
            /*
            下拉框操作
             */
            //访问百度
            driver.get("http://www.baidu.com");
            //等待
            Thread.sleep(2000);
            //定位到下拉框
            WebElement element = driver.findElement(By.tagName("select"));
            //下拉框对象
            Select select = new Select(element);
            //拿到下拉框里的元素
            List<WebElement> options =select.getOptions();
            for (WebElement option : options) {
                String tagname = option.getTagName();
                String text = option.getText();
                if ("女性".equals(text)){
                    //根据文本值选中下拉框中的女性选项
                    select.selectByVisibleText("女性");
                    //根据index选择下拉框中的选项
                    select.selectByIndex(0);
                    //根据value值选择下拉框中的选项,value值是某个下拉选项的value
                    select.selectByValue("value");
                }
            }
        }
    }

     

    鼠标键盘事件,拖拽

    package com.web_java01;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.Test;
    
    public class Action_web extends Base{
        @Test
        public void test() throws InterruptedException {
            //访问百度
            driver.get("http://www.baidu.com");
            //等待
            Thread.sleep(2000);
            //定位元素
            WebElement source = driver.findElement(By.id("kw"));
            WebElement target = driver.findElement(By.id("ww"));
            Actions actions = new Actions(driver);
            //拖拽source到target
            actions.dragAndDrop(source,target).build().perform();
    
        }
    }
  • 相关阅读:
    2020-03-1811:29:37springboot与任务
    2020-03-17 20:18:50springboot整合rabbitmq
    2020.03.17 springboot缓存相关
    前端JS面试
    npm 常用指令
    ES8新特性
    ES7新特性
    ES6新特性
    SpringBoot
    SpringBoot
  • 原文地址:https://www.cnblogs.com/zhaobobo001/p/14659655.html
Copyright © 2011-2022 走看看