selenium webdriver(4)—模拟鼠标键盘操作

webdriver提供Actions来模拟鼠标悬浮、拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Actions的相关操作 

输入数据                                                                                                                                                                      
 

需求:登录安居客网站,在二手房板块输入”@@@”,点击搜索,正确跳转成功反之失败,大部分情况下我们这样写

 

import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement;

public class NewTest{
  public static void main(String[] args) throws InterruptedException {

    System.setProperty ( "webdriver.chrome.driver" ,
    "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
    WebDriver driver = new ChromeDriver();
    driver.get("http://shanghai.anjuke.com");
    try{

      WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
      WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']"));

      //生成Actions实例对象
      Actions actions=new Actions(driver);
      //输入@@@点击搜索
      actions.keyDown(input, Keys.SHIFT).sendKeys("222").keyUp(Keys.SHIFT).click(search).perform();

      if(driver.getTitle().contains("@"))
        System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");
      else
        System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面");

    }catch(Exception e){
      e.printStackTrace();
    }finally{
      Thread.sleep(3000);
      driver.quit();
    }

  }
}

keyDown表示按下键盘,keyUp表示松开键盘。上述代码中先是执行keyDown,这时shift键按下后面的sendKeys内容也是在shift键按下的情况输入的,所以实际输入的是@@@.

鼠标悬浮                                                                                                                                                                     

需求:登录安居客首页,切换城市到杭州

 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;

public class NewTest{
  public static void main(String[] args) throws InterruptedException {

    System.setProperty ( "webdriver.chrome.driver" ,
    "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
    WebDriver driver = new ChromeDriver();
    driver.get("http://shanghai.anjuke.com");
    try{

      WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
      WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']"));
      input.sendKeys("@@@");
      search.click();
      if(driver.getTitle().contains("@"))
        System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");
      else
        System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面");

    }catch(Exception e){
      e.printStackTrace();
    }finally{
      Thread.sleep(3000);
      driver.quit();
  }
}

使用Actions类还可以这样写

package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Dimension;

public class testing {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    WebDriver driver = new FirefoxDriver();
    driver.get("http://anjuke.com");

    try {
      WebElement city = driver.findElement(By.id("switch_apf_id_8"));
      WebElement citys = driver.findElement(By.id("city-panel"));
      WebElement cityOption = driver.findElement(By.xpath("//*[@id='city-panel']/dl[2]/dd/a[2]"));

      //生成actions实例对象
      Actions actions = new Actions(driver);
      //鼠标悬浮到城市标签
      actions.moveToElement(city).perform();

      if(citys.isDisplayed()) {
        System.out.println("鼠标悬浮成功,城市模块的display:" + citys.getCssValue("display"));
      } else {
        System.out.println("鼠标悬浮失败,城市模块的display:" + citys.getCssValue("display"));
      }

      //点击杭州
      actions.click(cityOption).perform();

      if (driver.getTitle().contains("杭州")) {
        System.out.println("搜索成功" + driver.getTitle());
      } else {
        System.out.println("搜索失败" + driver.getTitle());

      }
    }catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    } finally {
      driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
      driver.quit();

    }



  }

}

 

posted on 2017-06-12 16:12  妮妮123  阅读(194)  评论(0编辑  收藏  举报

导航