zoukankan      html  css  js  c++  java
  • Java-WebDriver模块

    1、使用webdirver模块对浏览器进行操作

      WebDriver driver = new ChromeDriver();
      String baseUrl = "http://www.maiziedu.com/";
      driver.get(baseUrl); //打开麦子学院
      driver.getTitle() //得到当前网页的title driver.getTitle().indexOf("麦子") != -1
      driver.getCurrentUrl() //得到当前网页的url driver.getCurrentUrl().indexOf("maizi") != -1
      driver.manage().window().maximize(); //浏览器最大化
      driver.navigate().to("http://www.baidu.com"); //打开百度
      driver.navigate().refresh();//刷新浏览器
      driver.navigate().back();//浏览器后退
      driver.navigate().forward();//浏览器前进
      driver.close();//关闭当前页面
      driver.quit();//关闭所有窗口

    截图操作:

      driver.get(""http://www.baidu.com"");
      File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(srcFile, new File(""c:\1.png""));

    模拟鼠标操作:

      driver.get(""http://www.baidu.com"");
      Actions action = new Actions(driver);
      action.contextClick(driver.findElement(By.id(""kw""))).perform();
      //杀掉Windows浏览器进程
      WindowsUtils.tryToKillByName("chrome.exe");

    2、webdriver模块对web UI元素进行操作

    链接:

      WebElement element = driver.findElement(By.linkText(""小坦克""));
      element.click();

    输入框:

      driver.findElement(By.id(""userid"")).clear();//清空输入框
      driver.findElement(By.id(""userid"")).sendKeys(""test111111"");//输入内容
      driver.findElement(By.id(""userid"")).getAttribute(""value"");//获取输入框的内容"

    按钮:

      String xpath = ""//input[@value='添加']"";//value值为“添加”的input元素
      WebElement element = driver.findElement(By.xpath(xpath));//获取元素
      element.click();//点击
      element.isEnabled();//判断按钮是否可用

    下拉选者框:

      Select select = new Select(driver.findElement(By.id(""proAddItem"")));
      select.selectByIndex(2);//选择第三个选项,index从0开始
      select.selectByValue(""18"");//选择value=18的选项
      select.selectByVisibleText(""种类AA"");//选择文本值为“种类AA”的选项
      List<WebElement> options = select.getOptions();//获取所有的选项

    单选按钮:

      String xpath = ""//input[@type='radio'][@value='Apple']"";//taype=radio,value=Apple的input元素
      WebElement apple = driver.findElement(By.xpath(xpath));//获取元素
      apple.click();//选择单选框
      apple.isSelected();//单选框是否被选择
      apple.getAttribute(""value"");//获取元素属性

    多选框:

      String xpath = ""//input[@type='checkbox'][@value='Apple']"";//taype=checkbox,value=Apple的input元素
      String xpath1 = ""//input[@type='checkbox'][@value='Banana']"";//taype=checkbox,value=Apple的input元素
      WebElement apple = driver.findElement(By.xpath(xpath));//获取元素
      WebElement Banana = driver.findElement(By.xpath(xpath1));//获取元素
      apple.click();
      Banana.click();
      apple.isSelected();//多选框是否被选择
      Banana.isSelected();//多选框是否被选择
      apple.getAttribute(""value"");//获取元素属性
      Banana.getAttribute(""value"");//获取元素属性

    3、多窗口切换

      Set<String> allWindowsId = driver.getWindowHandles();//所有打开的窗口
      for (String windowId : allWindowsId) {
        if (driver.switchTo().window(windowId).getTitle().contains(""博客园"")) {
          driver.switchTo().window(windowId);//切换到目标窗口
          break;
        }
      }
      String parentWindowId = driver.getWindowHandle();//获取当前窗口
      driver.switchTo().window(parentWindowId);//切回到父窗口

    4、弹出框

      WebElement alertButton = driver.findElement(By.xpath(""//input[@value='alert']""));
      Alert alert = driver.switchTo().alert();
      driver.accept();
  • 相关阅读:
    关于DISCUZ!NT发布的问题整理!
    javascript 文字滚动显示[zhuan ]
    Bot Framework Emulator应用与调试
    Bot Framework的简单实现
    命名空间在扩展方法中的妙用
    Error:Execution failed for task ':app:transformClassesWithDexForDebug"
    C#中WebApi接口传参不再困惑:传参详解
    贝塞尔曲线
    从枚举值获取对应的文本描述
    常用正则表达式收集
  • 原文地址:https://www.cnblogs.com/lilyo/p/11959486.html
Copyright © 2011-2022 走看看