WebDriverAPI(6)

时间:2023-03-08 22:12:43

  在指定元素上方进行鼠标悬浮

  测试网址

  http://www.baidu.com

  Java语言版本实例 

  @Test
  public void roverOnElement() {
    driver.manage().window().maximize();
    driver.get(url);
    //找到页面链接元素赋值
    WebElement link1 = driver.findElement(By.xpath("//*[@id='u1']/a[1]"));
    WebElement link2 = driver.findElement(By.xpath("//*[@id='u1']/a[2]"));
    Actions action = new Actions(driver);
    //鼠标悬停
    action.moveToElement(link1).perform();
    try {
      Thread.sleep(3000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    action.moveToElement(link2).perform();
  }

  在指定元素上方进行鼠标左键和释放的操作

  测试网址

  http://www.w3school.com.cn/tiy/t.asp?f=html5_ev_onmousedown

  Java语言版本实例 

  @Test
  public void mouseClickAndRelease() {
    driver.manage().window().maximize();
    driver.get(url);
    //找到ifram框架的位置
    driver.switchTo().frame(1);
    WebElement div = driver.findElement(By.xpath("//*[@id='p1']"));
    Actions action = new Actions(driver);
    //按住鼠标左键不释放
    action.clickAndHold(div).perform();
    try {
      //暂停2秒可以看到左键持续点击的文字变为红色
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    //松开鼠标
    action.release(div).perform();
    try {
      //暂停2秒可以看到松开鼠标,字体变为绿色
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }