zoukankan      html  css  js  c++  java
  • selenium 元素操作(四)

    1. 输入框input

      输入:sendKeys( );

      清空:clear( );

      点击:click( );

    为了保证输入结果的准确,一般情况先清空,再输入

    2. 按钮button

      点击:click( );
      判断元素是否可点击:isEnable( );
      提交表单:,submit( ); click( )也可以用,有时候会报错

    3.上传文件

      sendKeys("c:\1.jpg"); 参数为文件的路径

    4.单选框,多选框

      click( );  选上
      clear( ); 取消选上
      isSelected( );  判断是否可选

    5.下拉选择框

      新建 Select对象,界面元素作为参数:Select select=new Select(element);

      选择值的方法

        按索引:select.selectByIndex(int index);
        按value值:select.selectByValue(String value);
        按可见文本:select.selectByVisibleText(String text);

      遍历下拉选项

    Select select = new Select(driver.findElement(By.id("idValue")));
    for(WebElement e : select.getOptions())
      e.click();
    }

    6.鼠标操作 ,使用 Actions 类

    WebElement input=driver.findElement(By.id("su"));
    Actions action=new Actions(driver);

      单击:action.click(input).perform();

      双击:action.doubleClick(input).perform();

      右击:action.contextClick((input).perform();

      鼠标悬停:action.moveToElement(input).perform();

      拖动:action.dragAndDrop((input).perform();

      在一个元素上按下鼠标左键:action.click_and_hold((input).perform();

    注意:Actions类的方法必须以 perform( ) 结尾,不然不会执行

    7.键盘操作

      删除一个字符:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.BACK_SPACE)

      输入“m”:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("m");

      输入空格:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.SPACE);

      全选输入框内容:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"a");

      复制:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"c");    

      剪切输入框内容:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"x");

      粘贴输入框内容 :driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.CONTROL,"v");

      通过回车来代替提交动作:driver.findElement(By.xpath("//*[@id='kw']")).sendKeys(Keys.ENTER);

  • 相关阅读:
    LeetCode -- Rectangle Area
    【转】VS常用快捷键
    C++中include<> 与 include" " 的区别
    浅析_tmain() 与 main() 函数的区别
    VS2013下配置OpenCV 3.0.0 &&& VS2013下配置Opencv2.4.9
    LeetCode -- Valid Parenthese
    杂想 · 警醒
    LeetCode -- Length of Last Word
    LeetCode -- Valid Sudoku
    LeetCode -- Binary Tree Level Order Traversal II
  • 原文地址:https://www.cnblogs.com/yjh1995/p/11986351.html
Copyright © 2011-2022 走看看