java-selenium键盘和鼠标

=

=

1.模拟键盘事件

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. Actions action = new Actions(driver);  
  2.         action.keyDown(Keys.CONTROL);  
  3.         action.keyDown(Keys.SHIFT);  
  4.         action.keyDown(Keys.ALT);  
  5.         action.keyUp(Keys.CONTROL);  
  6.         action.keyUp(Keys.SHIFT);  
  7.         action.keyUp(Keys.ALT);  
  8.           
  9.         action.keyDown(Keys.SHIFT).sendKeys("adc").perform();  

源码api:

    public Actions keyDown(Keys theKey) {
        return this.keyDown((WebElement)null, theKey);
    }

    public Actions keyDown(WebElement element, Keys theKey) {
        this.action.addAction(new KeyDownAction(this.keyboard, this.mouse, (Locatable)element, theKey));
        return this;
    }

2.鼠标:

鼠标左键:鼠标左键就是普通的click()函数

鼠标右键:

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. Actions action = new Actions(driver);  
  2.         action.contextClick(driver.findElement(By.id("query"))).perform();  


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

 

 

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2.     <head>  
  3.     <meta http-equiv="Content-type" content="text/html;charset=gb2312"/>  
  4.     <script language="javascript">  
  5.         function shownone()  
  6.         {  
  7.             document.getElementById('div1').style.display="none";  
  8.         }  
  9.         function showBlock()  
  10.         {  
  11.             document.getElementById('div1').style.display="block";  
  12.         }  
  13.     </script>  
  14.     <style type="text/css">  
  15.     <!--#div1{  
  16.     position:absolute;  
  17.     width:200px;height:115px;  
  18.     z-index:1;  
  19.     left 28px;top:34px;background-color:#0033CC;  
  20.     }-->  
  21.     </style>  
  22.     </head>  
  23. <body onload="shownone()">  
  24.         <div id ="div1"></div>  
  25.         <a onmouseover="showBlock()" onmouseout="shownone()" id="link1">鼠标指过来</a>  
  26.         <a onmouseover="showBlock()" onmouseout="shownone()" id="link2">鼠标指过来</a>  
  27. </body>  
  28. </html>  

selenium代码

 

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. WebElement link1 =driver.findElement(By.xpath("//a[@id='link1']"));  
  2.         WebElement link2 = driver.findElement(By.xpath("//a[@id='link2']"));  
  3.           
  4.         new Actions(driver).moveToElement(link1).perform();  
  5.           
  6.         try {  
  7.             Thread.sleep(3000);  
  8.         } catch (InterruptedException e) {  
  9.             // TODO: handle exception  
  10.             e.printStackTrace();  
  11.         }  
  12.           
  13.         new Actions(driver).moveToElement(link2).perform();  

=

=

猜你喜欢

转载自fantaxy025025.iteye.com/blog/2372450