Selenium键盘鼠标操作总结

鼠标操作

org.openqa.selenium.interactions.Actions 

1、给元素设置焦点。

有时对于a标签等,为了不跳转到别的链接,但是需要设置焦点时就可使用。

action.moveToElement(e); //移动鼠标到元素。
action.perform();//点击右键。

键盘操作

java.awt.Robot

1、输入各键盘值

(1)元素直接输入值。

  WebElement e=test.web.findElement(By.id("hfCityBox"));

  e.sendKeys("你好");

(2)Actions输入值。

Actions action = new Actions(test.web);

action.sendKeys(e, "ss");

 注:Actions 的 sendKeys(keysToSend) 执行完之后,焦点就不在当前元素了。

(3)Robot触发按键事件。

  Robot robot=null;
  try {
   robot = new Robot();
  } catch (AWTException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

robot.keyPress(KeyEvent.VK_0)//按下键盘0键。

robot.keyRelease(KeyEvent.VK_0);//放开键盘0键。

原文地址:https://www.cnblogs.com/swordyt/p/5375571.html