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

    }
}

 

posted @ 2021-04-14 20:05  小boboa  阅读(724)  评论(0编辑  收藏  举报