6-常见元素操作

1、文本框操作

sendkeys、clear、getAttribute

/**
 * 常见元素操作
 */
public class ActionSelenium {

    public WebDriver driver;

    @BeforeTest
    public void initDriver(){
        System.setProperty("webdriver.chrome.driver","E:\\study\\webuitest\\webuitest\\src\\driver\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.imooc.com/user/newlogin/from_url");
        //窗口最大化
        driver.manage().window().maximize();
    }

    /**
     * 文本框操作
     */
    @Test
    public void inputBox(){
        WebElement element = driver.findElement(By.name("email"));
        //往文本框输入内容
        element.sendKeys("123456");
        //清除内容
        element.clear();
        //获取属性名的属性值
        String value = element.getAttribute("placeholder");
        System.out.println(value);

    }
}

2、单选框操作

click、clear、isSelected

	/**
     * 单选框操作
     */
    @Test
    public void radioBox(){
        driver.get("https://www.imooc.com/user/setprofile");
        //点击编辑
        driver.findElement(By.className("pull-right")).click();
        //点击保密单选框
        WebElement element = driver.findElement(By.xpath("//*[@id=\"profile\"]/div[4]/div/label[1]/input"));
        element.click();
        //isSelected()  判断元素是否被选中
        Boolean a = element.isSelected();
        System.out.println(a);
    }

3、多选框

click、clear、isSelected、isEnabled

	/**
     * 多选框操作
     */
    @Test
    public void checkBox(){
        WebElement element = driver.findElement(By.id("auto-signin"));
        //判断元素是否被选中
        System.out.println(element.isSelected());
        //判断元素是否可用可编辑
        System.out.println(element.isEnabled());
        element.click();
        
    }

4、按钮操作

click、isEnabled

	/**
     * 按钮操作
     */
    @Test
    public void button(){
        WebElement element = driver.findElement(By.className("moco-btn"));
        //判断元素是否有效
        System.out.println(element.isEnabled());
        element.click();
    }

5、表单提交操作

submit

	/**
     * form表单操作---一般很少用,直接用click
     * 提交操作就是把数据发送到后台
     */
    @Test
    public void webForm(){
        driver.findElement(By.id("signup-form")).submit();
    }

6、上传文件操作

	/**
     * 上传文件
     */
    @Test
    public void upFile() throws InterruptedException {
        driver.get("https://www.imooc.com/user/setprofile");
        Thread.sleep(3000);     //等待3秒
        //更换头像按钮不可见,所以要更改css样式
        String jsStr = "document.getElementsByClassName('update-avator')[0].style.bottom='0'";
        //执行js脚本
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(jsStr);
        Thread.sleep(3000);
        //点击更换头像按钮
        driver.findElement(By.className("js-avator-link")).click();
        //点击上传头像
        driver.findElement((By.id("upload"))).sendKeys("E:\\study\\webuitest\\webuitest\\头像.jpeg");
        
    }

7、下拉框操作

用到selenium自带的一个类 Select list = new Select(locator)
(只有HTML页面中是select标签时,才能用下面这些方法)

选中对应的元素信息
Text、Value、Index

	/**
     * 下拉框操作
     */
    @Test
    public void selectBox() throws InterruptedException {
        driver.get("https://www.imooc.com/user/setprofile");
        //点击编辑按钮
        driver.findElement(By.className("pull-right")).click();
        Thread.sleep(1000);
        //定位职位下拉框,尝试了各种定位方式,是因为id-job有2个,所以这块定位不到,除非用xpath绝对路径
//        WebElement job = driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/form/div[2]/div/select"));
        //尝试用复合型定位方式,先定位上层元素,再定位子元素
        WebElement projob = driver.findElement(By.id("profile"));
        WebElement job = projob.findElement(By.id("job"));
        //判断元素是否可见
        System.out.println(job.isDisplayed());
        //对下拉框进行操作
        Select downlist = new Select(job);
        //通过索引操作
        downlist.selectByIndex(2);
        //通过value值操作
        downlist.selectByValue("5");
        //通过文本操作
        downlist.selectByVisibleText("移动开发工程师");

    }

8、鼠标操作

Actions action = new Acitons(driver);

鼠标单击 :不常见
鼠标右击:不常见
鼠标双击:不常见
鼠标悬停:很重要

	/**
     * 鼠标事件
     */
    @Test
    public void moseAction() throws InterruptedException {
        WebElement element = driver.findElement(By.className("moco-btn"));
        Actions actions = new Actions(driver);
        //鼠标单击
        actions.click(element).perform();
        //鼠标双击
        actions.doubleClick(element).perform();
        Thread.sleep(3000);
        //鼠标右击
        actions.contextClick(element).perform();
        Thread.sleep(3000);
        //鼠标悬停
        actions.moveToElement(element).perform();

    }

9、特殊窗体切换

9.1、iframe

在这里插入图片描述

9.2、弹窗

swhitchTo();
getWindowHandles();

10、延时等待

强制等待

//强制等待,设置固定休眠时间
Thread.sleep(3000);        //等待3秒

显式等待

//每隔10找一下id为text的元素
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("text")));

隐式等待

//不指定元素,是全局的等待
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值