这是本节的多页打印视图。
点击此处打印 .
返回本页常规视图 .
Actions接口 用于向 Web 浏览器提供虚拟化设备输入操作的低级接口.
除了高级元素交互 之外,
Actions 接口 
还提供了对指定输入设备
可以执行的确切操作的精细控制.
Selenium为3种输入源提供了接口:
键盘设备的键输入, 鼠标, 笔或触摸设备的输入, 以及滚轮设备的滚轮输入
(在Selenium 4.2中引入).
Selenium允许您构建分配给特定输入的独立操作命令,
会将他们链接在一起,
并调用关联的执行方法以一次执行它们.
Action构造器 在从遗留JSON Wire协议迁移到
新的W3C WebDriver协议的过程中,
低级的操作构建块变得特别详细.
它非常强大,
但每个输入设备都有多种使用方法,
如果您需要管理多个设备,
则负责确保他们之间的同步正确.
值得庆幸的是,
您可能不需要学习如何直接使用低级命令,
因为您可能要执行的几乎所有操作,
都已提供了相应的简便方法,
这些方法可以为您组合较低级别的命令.
请分别参阅相应的键盘 ,
鼠标 ,
笔 
和滚轮  页面.
暂停 指针移动和滚轮滚动
允许用户设置操作的持续时间,
但有时您只需要在操作之间等待一下,
即可正常工作.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . moveToElement ( clickable ) 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . clickAndHold () 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . sendKeys ( "abc" ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/ActionsTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 
 public   class  ActionsTest   extends   BaseChromeTest   { 
      @Test 
      public   void   pause ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          long   start   =   System . currentTimeMillis (); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . moveToElement ( clickable ) 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . clickAndHold () 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          long   duration   =   System . currentTimeMillis ()   -   start ; 
          Assertions . assertTrue ( duration   >   2000 ); 
          Assertions . assertTrue ( duration   <   3000 ); 
      } 
 
      @Test 
      public   void   releasesAll ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          Actions   actions   =   new   Actions ( driver ); 
          actions . clickAndHold ( clickable ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          (( RemoteWebDriver )   driver ). resetInputState (); 
 
          actions . sendKeys ( "a" ). perform (); 
          Assertions . assertEquals ( "A" ,   String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 0 ))); 
          Assertions . assertEquals ( "a" ,   String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 1 ))); 
      } 
 } 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver ) \
         . move_to_element ( clickable ) \
         . pause ( 1 ) \
         . click_and_hold () \
         . pause ( 1 ) \
         . send_keys ( "abc" ) \
         . perform ()  /examples/python/tests/actions_api/test_actions.py 
Copy
 
Close 
from  time  import  time 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.by  import  By 
 
 def  test_pauses ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     start  =  time () 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver ) \
         . move_to_element ( clickable ) \
         . pause ( 1 ) \
         . click_and_hold () \
         . pause ( 1 ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     duration  =  time ()  -  start 
     assert  duration  >  2 
     assert  duration  <  3 
 
 
 def  test_releases_all ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver ) \
         . click_and_hold ( clickable ) \
         . key_down ( Keys . SHIFT ) \
         . key_down ( "a" ) \
         . perform () 
 
     ActionBuilder ( driver ) . clear_actions () 
 
     ActionChains ( driver ) . key_down ( 'a' ) . perform () 
 
     assert  clickable . get_attribute ( 'value' )[ 0 ]  ==  "A" 
     assert  clickable . get_attribute ( 'value' )[ 1 ]  ==  "a" 
 Selenium v4.2 
            IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( clickable ) 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . ClickAndHold () 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . SendKeys ( "abc" ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/ActionsTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  ActionsTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  Pause () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             DateTime  start  =  DateTime . Now ; 
 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( clickable ) 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . ClickAndHold () 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             TimeSpan  duration  =  DateTime . Now  -  start ; 
             Assert . IsTrue ( duration  >  TimeSpan . FromSeconds ( 2 )); 
             Assert . IsTrue ( duration  <  TimeSpan . FromSeconds ( 3 )); 
         } 
 
         [TestMethod] 
        public  void  ReleaseAll () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             var  actions  =  new  Actions ( driver ); 
             actions . ClickAndHold ( clickable ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             (( WebDriver ) driver ). ResetInputState (); 
 
             actions . SendKeys ( "a" ). Perform (); 
             var  value  =  clickable . GetAttribute ( "value" ); 
             Assert . AreEqual ( "A" ,  value [.. 1 ]); 
             Assert . AreEqual ( "a" ,  value . Substring ( 1 ,  1 )); 
         } 
     } 
 } Selenium v4.2 
    clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . move_to ( clickable ) 
           . pause ( duration :  1 ) 
           . click_and_hold 
           . pause ( duration :  1 ) 
           . send_keys ( 'abc' ) 
           . perform  /examples/ruby/spec/actions_api/actions_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Actions'  do 
  let ( :driver )  {  start_session  } 
 
   it  'pauses'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
     start  =  Time . now 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . move_to ( clickable ) 
           . pause ( duration :  1 ) 
           . click_and_hold 
           . pause ( duration :  1 ) 
           . send_keys ( 'abc' ) 
           . perform 
 
     duration  =  Time . now  -  start 
     expect ( duration ) . to  be  >  2 
     expect ( duration ) . to  be  <  3 
   end 
 
   it  'releases all'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     action  =  driver . action 
                    . click_and_hold ( clickable ) 
                    . key_down ( :shift ) 
                    . key_down ( 'a' ) 
     action . perform 
 
     driver . action . release_actions 
 
     action . key_down ( 'a' ) . perform 
     expect ( clickable . attribute ( 'value' ) [ 0 ] ) . to  eq  'A' 
     expect ( clickable . attribute ( 'value' ) [- 1 ] ) . to  eq  'a' 
   end 
 end 
    const  clickable  =  await  driver . findElement ( By . id ( 'clickable' )) 
     await  driver . actions () 
       . move ({  origin :  clickable  }) 
       . pause ( 1000 ) 
       . press () 
       . pause ( 1000 ) 
       . sendKeys ( 'abc' ) 
       . perform () 
 /examples/javascript/test/actionsApi/actionsTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Pause and Release All Actions' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'Pause' ,  async  function ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     const  start  =  Date . now () 
 
     const  clickable  =  await  driver . findElement ( By . id ( 'clickable' )) 
     await  driver . actions () 
       . move ({  origin :  clickable  }) 
       . pause ( 1000 ) 
       . press () 
       . pause ( 1000 ) 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     const  end  =  Date . now ()  -  start 
     assert . ok ( end  >  2000 ) 
     assert . ok ( end  <  4000 ) 
   }) 
 
   it ( 'Clear' ,  async  function ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     const  clickable  =  driver . findElement ( By . id ( 'clickable' )) 
     await  driver . actions () 
       . click ( clickable ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     await  driver . actions (). clear () 
     await  driver . actions (). sendKeys ( 'a' ). perform () 
 
     const  value  =  await  clickable . getAttribute ( 'value' ) 
     assert . deepStrictEqual ( 'A' ,  value . substring ( 0 ,  1 )) 
     assert . deepStrictEqual ( 'a' ,  value . substring ( 1 ,  2 )) 
   }) 
 }) 
        Actions ( driver ) 
             . moveToElement ( clickable ) 
             . pause ( Duration . ofSeconds ( 1 )) 
             . clickAndHold () 
             . pause ( Duration . ofSeconds ( 1 )) 
             . sendKeys ( "abc" ) 
             . perform ()  
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
 class  ActionsTest  :  BaseTest ()  { 
     @Test 
     fun  pause ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  start  =  System . currentTimeMillis () 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
             . moveToElement ( clickable ) 
             . pause ( Duration . ofSeconds ( 1 )) 
             . clickAndHold () 
             . pause ( Duration . ofSeconds ( 1 )) 
             . sendKeys ( "abc" ) 
             . perform ()  
 
         val  duration  =  System . currentTimeMillis ()  -  start 
         Assertions . assertTrue ( duration  >  2000 ) 
         Assertions . assertTrue ( duration  <  4000 ) 
     } 
 
     @Test 
     fun  releasesAll ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         val  actions  =  Actions ( driver ) 
         actions . clickAndHold ( clickable ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         ( driver  as  RemoteWebDriver ). resetInputState () 
 
         actions . sendKeys ( "a" ). perform () 
         Assertions . assertEquals ( "A" ,  clickable . getAttribute ( "value" ) !! . get ( 0 ). toString ()) 
         Assertions . assertEquals ( "a" ,  clickable . getAttribute ( "value" ) !! . get ( 1 ). toString ()) 
     } 
 } 
释放所有Actions 需要注意的重要一点是,
驱动程序会记住整个会话中所有输入项的状态.
即使创建actions类的新实例,
按下的键和指针的位置
也将处于以前执行的操作离开它们的任何状态.
有一种特殊的方法来释放所有当前按下的键和指针按钮.
此方法在每种语言中的实现方式不同,
因为它不会使用perform方法执行.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          (( RemoteWebDriver )   driver ). resetInputState (); /examples/java/src/test/java/dev/selenium/actions_api/ActionsTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 
 public   class  ActionsTest   extends   BaseChromeTest   { 
      @Test 
      public   void   pause ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          long   start   =   System . currentTimeMillis (); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . moveToElement ( clickable ) 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . clickAndHold () 
                  . pause ( Duration . ofSeconds ( 1 )) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          long   duration   =   System . currentTimeMillis ()   -   start ; 
          Assertions . assertTrue ( duration   >   2000 ); 
          Assertions . assertTrue ( duration   <   3000 ); 
      } 
 
      @Test 
      public   void   releasesAll ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          Actions   actions   =   new   Actions ( driver ); 
          actions . clickAndHold ( clickable ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          (( RemoteWebDriver )   driver ). resetInputState (); 
 
          actions . sendKeys ( "a" ). perform (); 
          Assertions . assertEquals ( "A" ,   String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 0 ))); 
          Assertions . assertEquals ( "a" ,   String . valueOf ( clickable . getAttribute ( "value" ). charAt ( 1 ))); 
      } 
 } 
     ActionBuilder ( driver ) . clear_actions ()  /examples/python/tests/actions_api/test_actions.py 
Copy
 
Close 
from  time  import  time 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.by  import  By 
 
 def  test_pauses ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     start  =  time () 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver ) \
         . move_to_element ( clickable ) \
         . pause ( 1 ) \
         . click_and_hold () \
         . pause ( 1 ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     duration  =  time ()  -  start 
     assert  duration  >  2 
     assert  duration  <  3 
 
 
 def  test_releases_all ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver ) \
         . click_and_hold ( clickable ) \
         . key_down ( Keys . SHIFT ) \
         . key_down ( "a" ) \
         . perform () 
 
     ActionBuilder ( driver ) . clear_actions () 
 
     ActionChains ( driver ) . key_down ( 'a' ) . perform () 
 
     assert  clickable . get_attribute ( 'value' )[ 0 ]  ==  "A" 
     assert  clickable . get_attribute ( 'value' )[ 1 ]  ==  "a" 
             (( WebDriver ) driver ). ResetInputState ();  /examples/dotnet/SeleniumDocs/ActionsAPI/ActionsTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  ActionsTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  Pause () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             DateTime  start  =  DateTime . Now ; 
 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( clickable ) 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . ClickAndHold () 
                 . Pause ( TimeSpan . FromSeconds ( 1 )) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             TimeSpan  duration  =  DateTime . Now  -  start ; 
             Assert . IsTrue ( duration  >  TimeSpan . FromSeconds ( 2 )); 
             Assert . IsTrue ( duration  <  TimeSpan . FromSeconds ( 3 )); 
         } 
 
         [TestMethod] 
        public  void  ReleaseAll () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             var  actions  =  new  Actions ( driver ); 
             actions . ClickAndHold ( clickable ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             (( WebDriver ) driver ). ResetInputState (); 
 
             actions . SendKeys ( "a" ). Perform (); 
             var  value  =  clickable . GetAttribute ( "value" ); 
             Assert . AreEqual ( "A" ,  value [.. 1 ]); 
             Assert . AreEqual ( "a" ,  value . Substring ( 1 ,  1 )); 
         } 
     } 
 }     driver . action . release_actions  /examples/ruby/spec/actions_api/actions_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Actions'  do 
  let ( :driver )  {  start_session  } 
 
   it  'pauses'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
     start  =  Time . now 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . move_to ( clickable ) 
           . pause ( duration :  1 ) 
           . click_and_hold 
           . pause ( duration :  1 ) 
           . send_keys ( 'abc' ) 
           . perform 
 
     duration  =  Time . now  -  start 
     expect ( duration ) . to  be  >  2 
     expect ( duration ) . to  be  <  3 
   end 
 
   it  'releases all'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     action  =  driver . action 
                    . click_and_hold ( clickable ) 
                    . key_down ( :shift ) 
                    . key_down ( 'a' ) 
     action . perform 
 
     driver . action . release_actions 
 
     action . key_down ( 'a' ) . perform 
     expect ( clickable . attribute ( 'value' ) [ 0 ] ) . to  eq  'A' 
     expect ( clickable . attribute ( 'value' ) [- 1 ] ) . to  eq  'a' 
   end 
 end 
    await  driver . actions (). clear () 
 /examples/javascript/test/actionsApi/actionsTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Pause and Release All Actions' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'Pause' ,  async  function ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     const  start  =  Date . now () 
 
     const  clickable  =  await  driver . findElement ( By . id ( 'clickable' )) 
     await  driver . actions () 
       . move ({  origin :  clickable  }) 
       . pause ( 1000 ) 
       . press () 
       . pause ( 1000 ) 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     const  end  =  Date . now ()  -  start 
     assert . ok ( end  >  2000 ) 
     assert . ok ( end  <  4000 ) 
   }) 
 
   it ( 'Clear' ,  async  function ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     const  clickable  =  driver . findElement ( By . id ( 'clickable' )) 
     await  driver . actions () 
       . click ( clickable ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     await  driver . actions (). clear () 
     await  driver . actions (). sendKeys ( 'a' ). perform () 
 
     const  value  =  await  clickable . getAttribute ( 'value' ) 
     assert . deepStrictEqual ( 'A' ,  value . substring ( 0 ,  1 )) 
     assert . deepStrictEqual ( 'a' ,  value . substring ( 1 ,  2 )) 
   }) 
 }) 
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/ActionsTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
 class  ActionsTest  :  BaseTest ()  { 
     @Test 
     fun  pause ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  start  =  System . currentTimeMillis () 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
             . moveToElement ( clickable ) 
             . pause ( Duration . ofSeconds ( 1 )) 
             . clickAndHold () 
             . pause ( Duration . ofSeconds ( 1 )) 
             . sendKeys ( "abc" ) 
             . perform ()  
 
         val  duration  =  System . currentTimeMillis ()  -  start 
         Assertions . assertTrue ( duration  >  2000 ) 
         Assertions . assertTrue ( duration  <  4000 ) 
     } 
 
     @Test 
     fun  releasesAll ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         val  actions  =  Actions ( driver ) 
         actions . clickAndHold ( clickable ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         ( driver  as  RemoteWebDriver ). resetInputState () 
 
         actions . sendKeys ( "a" ). perform () 
         Assertions . assertEquals ( "A" ,  clickable . getAttribute ( "value" ) !! . get ( 0 ). toString ()) 
         Assertions . assertEquals ( "a" ,  clickable . getAttribute ( "value" ) !! . get ( 1 ). toString ()) 
     } 
 } 
1 - 键盘操作 一种适用于任何与网页交互的按键输入设备的表现形式.
只有 2 个操作可以使用键盘完成:
按下某个键,以及释放一个按下的键.
除了支持 ASCII 字符外,每个键盘按键还具有
可以按特定顺序按下或释放的表现形式.
按键 除了由常规unicode表示的按键,
其他键盘按键被分配了一些unicode值以用于操作Selenium
每种语言都有自己的方式来援引这些按键;
这里 
可以找到完整列表
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin 按下按键 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.Platform ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 
 public   class  KeysTest   extends   BaseChromeTest   { 
      @Test 
      public   void   keyDown ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "A" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   keyUp ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "Ab" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToActiveElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "abc" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToDesignatedElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
          driver . findElement ( By . tagName ( "body" )). click (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 
          Assertions . assertEquals ( "Selenium!" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   copyAndPaste ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); 
      } 
 } 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform ()  /examples/python/tests/actions_api/test_keys.py 
Copy
 
Close 
import  sys 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.by  import  By 
 
 def  test_key_down ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "ABC" 
 
 
 def  test_key_up ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "Ab" 
 
 
 def  test_send_keys_to_active_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_send_keys_to_designated_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     driver . find_element ( By . TAG_NAME ,  "body" ) . click () 
 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_copy_and_paste ( firefox_driver ): 
    driver  =  firefox_driver 
     driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "SeleniumSelenium!" 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  KeysTest  :  BaseFirefoxTest 
     { 
         [TestMethod] 
        public  void  KeyDown () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "A" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  KeyUp () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "Ab" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToActiveElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToDesignatedElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver ) 
                 . SendKeys ( textField ,  "abc" ) 
                 . Perform (); 
 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  CopyAndPaste () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp ) 
                 . KeyUp ( Keys . Shift ) 
                 . KeyDown ( cmdCtrl ) 
                 . SendKeys ( "xvv" ) 
                 . KeyUp ( cmdCtrl ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "SeleniumSelenium!" ,  textField . GetAttribute ( "value" )); 
         } 
     } 
 }     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform  /examples/ruby/spec/actions_api/keys_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Keys'  do 
  let ( :driver )  {  start_session  } 
   let ( :wait )  {  Selenium :: WebDriver :: Wait . new ( timeout :  2 )  } 
 
   it  'key down'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'A' 
   end 
 
   it  'key up'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'Ab' 
   end 
 
   it  'sends keys to active element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . send_keys ( 'abc' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'abc' 
   end 
 
   it  'sends keys to designated element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     driver . find_element ( tag_name :  'body' ) . click 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform 
 
     expect ( text_field . attribute ( 'value' )) . to  eq  'Selenium!' 
   end 
 
   it  'copy and paste'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     cmd_ctrl  =  driver . capabilities . platform_name . include? ( 'mac' )  ?  :command  :  :control 
     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'SeleniumSelenium!' 
   end 
 end 
    await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 /examples/javascript/test/actionsApi/keysTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
const  {  platform  }  =  require ( 'node:process' ) 
 describe ( 'Keyboard Action - Keys test' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'KeyDown' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'A' ) 
   }) 
 
   it ( 'KeyUp' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'Ab' ) 
   }) 
 
   it ( 'sendKeys' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Designated Element' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . findElement ( By . css ( 'body' )). click () 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Copy and Paste' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'SeleniumSelenium!' ) 
   }) 
 }) 
                . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.HasCapabilities 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.Platform 
import  org.openqa.selenium.interactions.Actions 
 class  KeysTest  :  BaseTest ()  { 
     @Test 
     fun  keyDown ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "A" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  keyUp ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "Ab" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToActiveElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . sendKeys ( "abc" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "abc" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToDesignatedElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
         driver . findElement ( By . tagName ( "body" )). click () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 
         Assertions . assertEquals ( "Selenium!" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  copyAndPaste ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         val  platformName  =  ( driver  as  HasCapabilities ). getCapabilities (). getPlatformName () 
 
         val  cmdCtrl  =  if ( platformName  ==  Platform . MAC )  Keys . COMMAND  else  Keys . CONTROL 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 
         Assertions . assertEquals ( "SeleniumSelenium!" ,  textField . getAttribute ( "value" )) 
     } 
 } 
释放按键 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.Platform ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 
 public   class  KeysTest   extends   BaseChromeTest   { 
      @Test 
      public   void   keyDown ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "A" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   keyUp ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "Ab" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToActiveElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "abc" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToDesignatedElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
          driver . findElement ( By . tagName ( "body" )). click (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 
          Assertions . assertEquals ( "Selenium!" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   copyAndPaste ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); 
      } 
 } 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform ()  /examples/python/tests/actions_api/test_keys.py 
Copy
 
Close 
import  sys 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.by  import  By 
 
 def  test_key_down ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "ABC" 
 
 
 def  test_key_up ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "Ab" 
 
 
 def  test_send_keys_to_active_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_send_keys_to_designated_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     driver . find_element ( By . TAG_NAME ,  "body" ) . click () 
 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_copy_and_paste ( firefox_driver ): 
    driver  =  firefox_driver 
     driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "SeleniumSelenium!" 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  KeysTest  :  BaseFirefoxTest 
     { 
         [TestMethod] 
        public  void  KeyDown () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "A" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  KeyUp () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "Ab" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToActiveElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToDesignatedElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver ) 
                 . SendKeys ( textField ,  "abc" ) 
                 . Perform (); 
 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  CopyAndPaste () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp ) 
                 . KeyUp ( Keys . Shift ) 
                 . KeyDown ( cmdCtrl ) 
                 . SendKeys ( "xvv" ) 
                 . KeyUp ( cmdCtrl ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "SeleniumSelenium!" ,  textField . GetAttribute ( "value" )); 
         } 
     } 
 }     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform  /examples/ruby/spec/actions_api/keys_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Keys'  do 
  let ( :driver )  {  start_session  } 
   let ( :wait )  {  Selenium :: WebDriver :: Wait . new ( timeout :  2 )  } 
 
   it  'key down'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'A' 
   end 
 
   it  'key up'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'Ab' 
   end 
 
   it  'sends keys to active element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . send_keys ( 'abc' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'abc' 
   end 
 
   it  'sends keys to designated element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     driver . find_element ( tag_name :  'body' ) . click 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform 
 
     expect ( text_field . attribute ( 'value' )) . to  eq  'Selenium!' 
   end 
 
   it  'copy and paste'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     cmd_ctrl  =  driver . capabilities . platform_name . include? ( 'mac' )  ?  :command  :  :control 
     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'SeleniumSelenium!' 
   end 
 end 
    await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 /examples/javascript/test/actionsApi/keysTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
const  {  platform  }  =  require ( 'node:process' ) 
 describe ( 'Keyboard Action - Keys test' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'KeyDown' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'A' ) 
   }) 
 
   it ( 'KeyUp' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'Ab' ) 
   }) 
 
   it ( 'sendKeys' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Designated Element' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . findElement ( By . css ( 'body' )). click () 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Copy and Paste' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'SeleniumSelenium!' ) 
   }) 
 }) 
                . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.HasCapabilities 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.Platform 
import  org.openqa.selenium.interactions.Actions 
 class  KeysTest  :  BaseTest ()  { 
     @Test 
     fun  keyDown ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "A" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  keyUp ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "Ab" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToActiveElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . sendKeys ( "abc" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "abc" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToDesignatedElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
         driver . findElement ( By . tagName ( "body" )). click () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 
         Assertions . assertEquals ( "Selenium!" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  copyAndPaste ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         val  platformName  =  ( driver  as  HasCapabilities ). getCapabilities (). getPlatformName () 
 
         val  cmdCtrl  =  if ( platformName  ==  Platform . MAC )  Keys . COMMAND  else  Keys . CONTROL 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 
         Assertions . assertEquals ( "SeleniumSelenium!" ,  textField . getAttribute ( "value" )) 
     } 
 } 
键入 这是Actions API的一种便捷方法,
它将 keyDown 和 keyUp 命令组合在一个操作中.
执行此命令与使用 element 方法略有不同,
但这主要用于,需要在其他操作之间键入多个字符时使用.
活跃元素 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.Platform ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 
 public   class  KeysTest   extends   BaseChromeTest   { 
      @Test 
      public   void   keyDown ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "A" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   keyUp ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "Ab" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToActiveElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "abc" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToDesignatedElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
          driver . findElement ( By . tagName ( "body" )). click (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 
          Assertions . assertEquals ( "Selenium!" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   copyAndPaste ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); 
      } 
 } 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform ()  /examples/python/tests/actions_api/test_keys.py 
Copy
 
Close 
import  sys 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.by  import  By 
 
 def  test_key_down ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "ABC" 
 
 
 def  test_key_up ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "Ab" 
 
 
 def  test_send_keys_to_active_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_send_keys_to_designated_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     driver . find_element ( By . TAG_NAME ,  "body" ) . click () 
 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_copy_and_paste ( firefox_driver ): 
    driver  =  firefox_driver 
     driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "SeleniumSelenium!" 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" )  /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  KeysTest  :  BaseFirefoxTest 
     { 
         [TestMethod] 
        public  void  KeyDown () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "A" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  KeyUp () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "Ab" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToActiveElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToDesignatedElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver ) 
                 . SendKeys ( textField ,  "abc" ) 
                 . Perform (); 
 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  CopyAndPaste () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp ) 
                 . KeyUp ( Keys . Shift ) 
                 . KeyDown ( cmdCtrl ) 
                 . SendKeys ( "xvv" ) 
                 . KeyUp ( cmdCtrl ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "SeleniumSelenium!" ,  textField . GetAttribute ( "value" )); 
         } 
     } 
 }     driver . action 
           . send_keys ( 'abc' ) 
           . perform  /examples/ruby/spec/actions_api/keys_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Keys'  do 
  let ( :driver )  {  start_session  } 
   let ( :wait )  {  Selenium :: WebDriver :: Wait . new ( timeout :  2 )  } 
 
   it  'key down'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'A' 
   end 
 
   it  'key up'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'Ab' 
   end 
 
   it  'sends keys to active element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . send_keys ( 'abc' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'abc' 
   end 
 
   it  'sends keys to designated element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     driver . find_element ( tag_name :  'body' ) . click 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform 
 
     expect ( text_field . attribute ( 'value' )) . to  eq  'Selenium!' 
   end 
 
   it  'copy and paste'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     cmd_ctrl  =  driver . capabilities . platform_name . include? ( 'mac' )  ?  :command  :  :control 
     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'SeleniumSelenium!' 
   end 
 end 
    await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 /examples/javascript/test/actionsApi/keysTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
const  {  platform  }  =  require ( 'node:process' ) 
 describe ( 'Keyboard Action - Keys test' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'KeyDown' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'A' ) 
   }) 
 
   it ( 'KeyUp' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'Ab' ) 
   }) 
 
   it ( 'sendKeys' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Designated Element' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . findElement ( By . css ( 'body' )). click () 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Copy and Paste' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'SeleniumSelenium!' ) 
   }) 
 }) 
                . sendKeys ( "abc" ) 
                 . perform () 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.HasCapabilities 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.Platform 
import  org.openqa.selenium.interactions.Actions 
 class  KeysTest  :  BaseTest ()  { 
     @Test 
     fun  keyDown ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "A" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  keyUp ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "Ab" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToActiveElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . sendKeys ( "abc" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "abc" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToDesignatedElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
         driver . findElement ( By . tagName ( "body" )). click () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 
         Assertions . assertEquals ( "Selenium!" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  copyAndPaste ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         val  platformName  =  ( driver  as  HasCapabilities ). getCapabilities (). getPlatformName () 
 
         val  cmdCtrl  =  if ( platformName  ==  Platform . MAC )  Keys . COMMAND  else  Keys . CONTROL 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 
         Assertions . assertEquals ( "SeleniumSelenium!" ,  textField . getAttribute ( "value" )) 
     } 
 } 
指定元素 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.Platform ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 
 public   class  KeysTest   extends   BaseChromeTest   { 
      @Test 
      public   void   keyDown ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "A" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   keyUp ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "Ab" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToActiveElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "abc" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToDesignatedElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
          driver . findElement ( By . tagName ( "body" )). click (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 
          Assertions . assertEquals ( "Selenium!" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   copyAndPaste ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); 
      } 
 } 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform ()  /examples/python/tests/actions_api/test_keys.py 
Copy
 
Close 
import  sys 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.by  import  By 
 
 def  test_key_down ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "ABC" 
 
 
 def  test_key_up ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "Ab" 
 
 
 def  test_send_keys_to_active_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_send_keys_to_designated_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     driver . find_element ( By . TAG_NAME ,  "body" ) . click () 
 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_copy_and_paste ( firefox_driver ): 
    driver  =  firefox_driver 
     driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "SeleniumSelenium!" 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver )  /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  KeysTest  :  BaseFirefoxTest 
     { 
         [TestMethod] 
        public  void  KeyDown () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "A" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  KeyUp () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "Ab" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToActiveElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToDesignatedElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver ) 
                 . SendKeys ( textField ,  "abc" ) 
                 . Perform (); 
 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  CopyAndPaste () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp ) 
                 . KeyUp ( Keys . Shift ) 
                 . KeyDown ( cmdCtrl ) 
                 . SendKeys ( "xvv" ) 
                 . KeyUp ( cmdCtrl ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "SeleniumSelenium!" ,  textField . GetAttribute ( "value" )); 
         } 
     } 
 }     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform  /examples/ruby/spec/actions_api/keys_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Keys'  do 
  let ( :driver )  {  start_session  } 
   let ( :wait )  {  Selenium :: WebDriver :: Wait . new ( timeout :  2 )  } 
 
   it  'key down'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'A' 
   end 
 
   it  'key up'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'Ab' 
   end 
 
   it  'sends keys to active element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . send_keys ( 'abc' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'abc' 
   end 
 
   it  'sends keys to designated element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     driver . find_element ( tag_name :  'body' ) . click 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform 
 
     expect ( text_field . attribute ( 'value' )) . to  eq  'Selenium!' 
   end 
 
   it  'copy and paste'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     cmd_ctrl  =  driver . capabilities . platform_name . include? ( 'mac' )  ?  :command  :  :control 
     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'SeleniumSelenium!' 
   end 
 end 
Selenium v4.5.0 
    const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 /examples/javascript/test/actionsApi/keysTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
const  {  platform  }  =  require ( 'node:process' ) 
 describe ( 'Keyboard Action - Keys test' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'KeyDown' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'A' ) 
   }) 
 
   it ( 'KeyUp' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'Ab' ) 
   }) 
 
   it ( 'sendKeys' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Designated Element' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . findElement ( By . css ( 'body' )). click () 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Copy and Paste' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'SeleniumSelenium!' ) 
   }) 
 }) 
        Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.HasCapabilities 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.Platform 
import  org.openqa.selenium.interactions.Actions 
 class  KeysTest  :  BaseTest ()  { 
     @Test 
     fun  keyDown ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "A" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  keyUp ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "Ab" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToActiveElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . sendKeys ( "abc" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "abc" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToDesignatedElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
         driver . findElement ( By . tagName ( "body" )). click () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 
         Assertions . assertEquals ( "Selenium!" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  copyAndPaste ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         val  platformName  =  ( driver  as  HasCapabilities ). getCapabilities (). getPlatformName () 
 
         val  cmdCtrl  =  if ( platformName  ==  Platform . MAC )  Keys . COMMAND  else  Keys . CONTROL 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 
         Assertions . assertEquals ( "SeleniumSelenium!" ,  textField . getAttribute ( "value" )) 
     } 
 } 
复制粘贴 下面是使用上述所有方法执行复制/粘贴操作的示例.
请注意, 用于此操作的键位会有所不同, 具体取决于它是否是 Mac OS.
此代码将以文本收尾: SeleniumSelenium!
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); /examples/java/src/test/java/dev/selenium/actions_api/KeysTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Keys ; 
 import   org.openqa.selenium.Platform ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 
 public   class  KeysTest   extends   BaseChromeTest   { 
      @Test 
      public   void   keyDown ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "A" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   keyUp ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( "a" ) 
                  . keyUp ( Keys . SHIFT ) 
                  . sendKeys ( "b" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "Ab" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToActiveElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          new   Actions ( driver ) 
                  . sendKeys ( "abc" ) 
                  . perform (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          Assertions . assertEquals ( "abc" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   sendKeysToDesignatedElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
          driver . findElement ( By . tagName ( "body" )). click (); 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . perform (); 
 
          Assertions . assertEquals ( "Selenium!" ,   textField . getAttribute ( "value" )); 
      } 
 
      @Test 
      public   void   copyAndPaste ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ); 
 
          Keys   cmdCtrl   =   Platform . getCurrent (). is ( Platform . MAC )   ?   Keys . COMMAND   :   Keys . CONTROL ; 
 
          WebElement   textField   =   driver . findElement ( By . id ( "textInput" )); 
          new   Actions ( driver ) 
                  . sendKeys ( textField ,   "Selenium!" ) 
                  . sendKeys ( Keys . ARROW_LEFT ) 
                  . keyDown ( Keys . SHIFT ) 
                  . sendKeys ( Keys . ARROW_UP ) 
                  . keyUp ( Keys . SHIFT ) 
                  . keyDown ( cmdCtrl ) 
                  . sendKeys ( "xvv" ) 
                  . keyUp ( cmdCtrl ) 
                  . perform (); 
 
          Assertions . assertEquals ( "SeleniumSelenium!" ,   textField . getAttribute ( "value" )); 
      } 
 } 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform ()  /examples/python/tests/actions_api/test_keys.py 
Copy
 
Close 
import  sys 
 from  selenium.webdriver  import  Keys ,  ActionChains 
from  selenium.webdriver.common.by  import  By 
 
 def  test_key_down ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "ABC" 
 
 
 def  test_key_up ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( "a" ) \
         . key_up ( Keys . SHIFT ) \
         . send_keys ( "b" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "Ab" 
 
 
 def  test_send_keys_to_active_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
 
     ActionChains ( driver ) \
         . send_keys ( "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_send_keys_to_designated_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     driver . find_element ( By . TAG_NAME ,  "body" ) . click () 
 
     text_input  =  driver . find_element ( By . ID ,  "textInput" ) 
     ActionChains ( driver ) \
         . send_keys_to_element ( text_input ,  "abc" ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "abc" 
 
 
 def  test_copy_and_paste ( firefox_driver ): 
    driver  =  firefox_driver 
     driver . get ( 'https://selenium.dev/selenium/web/single_text_input.html' ) 
     cmd_ctrl  =  Keys . COMMAND  if  sys . platform  ==  'darwin'  else  Keys . CONTROL 
 
     ActionChains ( driver ) \
         . send_keys ( "Selenium!" ) \
         . send_keys ( Keys . ARROW_LEFT ) \
         . key_down ( Keys . SHIFT ) \
         . send_keys ( Keys . ARROW_UP ) \
         . key_up ( Keys . SHIFT ) \
         . key_down ( cmd_ctrl ) \
         . send_keys ( "xvv" ) \
         . key_up ( cmd_ctrl ) \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "textInput" ) . get_attribute ( 'value' )  ==  "SeleniumSelenium!" 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp )  /examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  KeysTest  :  BaseFirefoxTest 
     { 
         [TestMethod] 
        public  void  KeyDown () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "A" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  KeyUp () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( "a" ) 
                 . KeyUp ( Keys . Shift ) 
                 . SendKeys ( "b" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "Ab" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToActiveElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "abc" ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
         
         [TestMethod] 
        public  void  SendKeysToDesignatedElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
             driver . FindElement ( By . TagName ( "body" )). Click (); 
             
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             new  Actions ( driver ) 
                 . SendKeys ( textField ,  "abc" ) 
                 . Perform (); 
 
             Assert . AreEqual ( "abc" ,  textField . GetAttribute ( "value" )); 
         } 
 
         [TestMethod] 
        public  void  CopyAndPaste () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/single_text_input.html" ; 
 
             var  capabilities  =  (( WebDriver ) driver ). Capabilities ; 
             String  platformName  =  ( string ) capabilities . GetCapability ( "platformName" ); 
 
             String  cmdCtrl  =  platformName . Contains ( "mac" )  ?  Keys . Command  :  Keys . Control ; 
 
             new  Actions ( driver ) 
                 . SendKeys ( "Selenium!" ) 
                 . SendKeys ( Keys . ArrowLeft ) 
                 . KeyDown ( Keys . Shift ) 
                 . SendKeys ( Keys . ArrowUp ) 
                 . KeyUp ( Keys . Shift ) 
                 . KeyDown ( cmdCtrl ) 
                 . SendKeys ( "xvv" ) 
                 . KeyUp ( cmdCtrl ) 
                 . Perform (); 
 
             IWebElement  textField  =  driver . FindElement ( By . Id ( "textInput" )); 
             Assert . AreEqual ( "SeleniumSelenium!" ,  textField . GetAttribute ( "value" )); 
         } 
     } 
 }     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 /examples/ruby/spec/actions_api/keys_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Keys'  do 
  let ( :driver )  {  start_session  } 
   let ( :wait )  {  Selenium :: WebDriver :: Wait . new ( timeout :  2 )  } 
 
   it  'key down'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'A' 
   end 
 
   it  'key up'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . key_down ( :shift ) 
           . send_keys ( 'a' ) 
           . key_up ( :shift ) 
           . send_keys ( 'b' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'Ab' 
   end 
 
   it  'sends keys to active element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     driver . action 
           . send_keys ( 'abc' ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'abc' 
   end 
 
   it  'sends keys to designated element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     driver . find_element ( tag_name :  'body' ) . click 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     text_field  =  driver . find_element ( id :  'textInput' ) 
     driver . action 
           . send_keys ( text_field ,  'Selenium!' ) 
           . perform 
 
     expect ( text_field . attribute ( 'value' )) . to  eq  'Selenium!' 
   end 
 
   it  'copy and paste'  do 
     driver . get  'https://www.selenium.dev/selenium/web/single_text_input.html' 
     wait . until  {  driver . find_element ( id :  'textInput' ) . attribute ( 'autofocus' )  } 
 
     cmd_ctrl  =  driver . capabilities . platform_name . include? ( 'mac' )  ?  :command  :  :control 
     driver . action 
           . send_keys ( 'Selenium!' ) 
           . send_keys ( :arrow_left ) 
           . key_down ( :shift ) 
           . send_keys ( :arrow_up ) 
           . key_up ( :shift ) 
           . key_down ( cmd_ctrl ) 
           . send_keys ( 'xvv' ) 
           . key_up ( cmd_ctrl ) 
           . perform 
 
     expect ( driver . find_element ( id :  'textInput' ) . attribute ( 'value' )) . to  eq  'SeleniumSelenium!' 
   end 
 end 
    const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 /examples/javascript/test/actionsApi/keysTest.spec.js 
Copy
 
Close 
const  {  By ,  Key ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
const  {  platform  }  =  require ( 'node:process' ) 
 describe ( 'Keyboard Action - Keys test' ,  function ()  { 
  let  driver 
 
   before ( async  function ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async  ()  =>  await  driver . quit ()) 
 
   it ( 'KeyDown' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . perform () 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'A' ) 
   }) 
 
   it ( 'KeyUp' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( 'a' ) 
       . keyUp ( Key . SHIFT ) 
       . sendKeys ( 'b' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'Ab' ) 
   }) 
 
   it ( 'sendKeys' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  driver . findElement ( By . id ( 'textInput' )) 
     await  textField . click () 
 
     await  driver . actions () 
       . sendKeys ( 'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Designated Element' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     await  driver . findElement ( By . css ( 'body' )). click () 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     await  driver . actions () 
       . sendKeys ( textField ,  'abc' ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'abc' ) 
   }) 
 
   it ( 'Copy and Paste' ,  async  function ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/single_text_input.html' ) 
 
     const  textField  =  await  driver . findElement ( By . id ( 'textInput' )) 
 
     const  cmdCtrl  =  platform . includes ( 'darwin' )  ?  Key . COMMAND  :  Key . CONTROL 
 
     await  driver . actions () 
       . click ( textField ) 
       . sendKeys ( 'Selenium!' ) 
       . sendKeys ( Key . ARROW_LEFT ) 
       . keyDown ( Key . SHIFT ) 
       . sendKeys ( Key . ARROW_UP ) 
       . keyUp ( Key . SHIFT ) 
       . keyDown ( cmdCtrl ) 
       . sendKeys ( 'xvv' ) 
       . keyUp ( cmdCtrl ) 
       . perform () 
 
     assert . deepStrictEqual ( await  textField . getAttribute ( 'value' ),  'SeleniumSelenium!' ) 
   }) 
 }) 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/KeysTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.HasCapabilities 
import  org.openqa.selenium.Keys 
import  org.openqa.selenium.Platform 
import  org.openqa.selenium.interactions.Actions 
 class  KeysTest  :  BaseTest ()  { 
     @Test 
     fun  keyDown ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "A" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  keyUp ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( "a" ) 
                 . keyUp ( Keys . SHIFT ) 
                 . sendKeys ( "b" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "Ab" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToActiveElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         Actions ( driver ) 
                 . sendKeys ( "abc" ) 
                 . perform () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Assertions . assertEquals ( "abc" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  sendKeysToDesignatedElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
         driver . findElement ( By . tagName ( "body" )). click () 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . perform () 
 
         Assertions . assertEquals ( "Selenium!" ,  textField . getAttribute ( "value" )) 
     } 
 
     @Test 
     fun  copyAndPaste ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/single_text_input.html" ) 
 
         val  platformName  =  ( driver  as  HasCapabilities ). getCapabilities (). getPlatformName () 
 
         val  cmdCtrl  =  if ( platformName  ==  Platform . MAC )  Keys . COMMAND  else  Keys . CONTROL 
 
         val  textField  =  driver . findElement ( By . id ( "textInput" )) 
         Actions ( driver ) 
                 . sendKeys ( textField ,  "Selenium!" ) 
                 . sendKeys ( Keys . ARROW_LEFT ) 
                 . keyDown ( Keys . SHIFT ) 
                 . sendKeys ( Keys . ARROW_UP ) 
                 . keyUp ( Keys . SHIFT ) 
                 . keyDown ( cmdCtrl ) 
                 . sendKeys ( "xvv" ) 
                 . keyUp ( cmdCtrl ) 
                 . perform () 
 
         Assertions . assertEquals ( "SeleniumSelenium!" ,  textField . getAttribute ( "value" )) 
     } 
 } 
2 - 鼠标操作 任何用于与网页进行交互的指针设备的表示形式.
一个鼠标仅可以完成3个操作:
按住按钮,松开按钮,还有移动光标。
Selenium组合了常见的操作并提供了方便的方法。
按住鼠标左键 这个方法包含2个操作,首先将光标移动到被操作元素的正中心,然后按下鼠标左键不松开。
这对于聚焦一个特殊元素很有用:
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    let  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  clickable }). press (). perform ();  /examples/javascript/test/actionsApi/mouse/clickAndHold.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
 describe ( 'Click and hold' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Mouse move and mouseDown on an element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     let  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  clickable }). press (). perform (); 
   }); 
 }); 
                . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
点击鼠标左键 这个方法包含2个操作,首先将光标移动到被操作元素的正中心,然后按下鼠标左键后再松开。
另一种叫法“点击”:
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    let  click  =  driver . findElement ( By . id ( "click" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  click }). click (). perform ();  /examples/javascript/test/actionsApi/mouse/clickAndRelease.spec.js 
Copy
 
Close 
const  { By , Builder }  =  require ( 'selenium-webdriver' ); 
 describe ( 'Click and release' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Mouse move and click on an element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     let  click  =  driver . findElement ( By . id ( "click" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  click }). click (). perform (); 
   }); 
 }); 
                . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" ))  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
点击鼠标备用按钮 鼠标一共有5个定义好的按钮:
0 — 左键 (默认值) 1 — 中间键 (当前不支持) 2 — 右键 3 — X1 (返回) 按钮 4 — X2 (前进) 按钮 点击鼠标右键 这个方法包含2个操作,首先将光标移动到被操作元素的正中心,然后点击鼠标右键。
另一种叫法“点击右键”:
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . contextClick ( clickable ). perform ();  /examples/javascript/test/actionsApi/mouse/rightClick.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Right click' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Mouse move and right click on an element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . contextClick ( clickable ). perform (); 
 
     await  driver . sleep ( 500 ); 
     const  clicked  =  await  driver . findElement ( By . id ( 'click-status' )). getText (); 
     assert . deepStrictEqual ( clicked ,  `context-clicked` ) 
   }); 
 }); 
                . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
点击鼠标回退键 除了这个没有更方便的方法,只是点击鼠标回退按钮
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
 Selenium v4.2 
    action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 Selenium v4.2 
            ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 } Selenium v4.2 
      driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
Selenium v4.5.0 
    const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . BACK ). release ( Button . BACK ). perform ()  /examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js 
Copy
 
Close 
const  { By ,  Button ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Should be able to perform BACK click and FORWARD click' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Back click' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     await  driver . findElement ( By . id ( "click" )). click (); 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `We Arrive Here` ) 
 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . BACK ). release ( Button . BACK ). perform () 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `BasicMouseInterfaceTest` ) 
   }); 
 
   it ( 'Forward click' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     await  driver . findElement ( By . id ( "click" )). click (); 
     await  driver . navigate (). back (); 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `BasicMouseInterfaceTest` ) 
 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform () 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `We Arrive Here` ) 
   }); 
 }); 
        val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
点击鼠标前进键 除了这个没有更方便的方法,只是点击鼠标前进按钮
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
 Selenium v4.2 
    action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 Selenium v4.2 
            ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 } Selenium v4.2 
      driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
Selenium v4.5.0 
    const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform ()  /examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js 
Copy
 
Close 
const  { By ,  Button ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Should be able to perform BACK click and FORWARD click' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Back click' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     await  driver . findElement ( By . id ( "click" )). click (); 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `We Arrive Here` ) 
 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . BACK ). release ( Button . BACK ). perform () 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `BasicMouseInterfaceTest` ) 
   }); 
 
   it ( 'Forward click' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     await  driver . findElement ( By . id ( "click" )). click (); 
     await  driver . navigate (). back (); 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `BasicMouseInterfaceTest` ) 
 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . press ( Button . FORWARD ). release ( Button . FORWARD ). perform () 
 
     assert . deepStrictEqual ( await  driver . getTitle (),  `We Arrive Here` ) 
   }); 
 }); 
        val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
双击鼠标左键 这个方法包含2个操作,首先将光标移动到被操作元素的正中心,然后双击鼠标左键。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . doubleClick ( clickable ). perform ();  /examples/javascript/test/actionsApi/mouse/doubleClick.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( "assert" ); 
 describe ( 'Double click' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Double-click on an element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  clickable  =  driver . findElement ( By . id ( "clickable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . doubleClick ( clickable ). perform (); 
 
     await  driver . sleep ( 500 ); 
     const  status  =  await  driver . findElement ( By . id ( 'click-status' )). getText (); 
     assert . deepStrictEqual ( status ,  `double-clicked` ) 
   }); 
 }); 
                . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
移动光标到元素上 这个方法是将光标移动到元素的中心点。
另一种叫法“悬停”。
元素必须在可视窗口范围内否则这条命令将会报错。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  hoverable  =  driver . findElement ( By . id ( "hover" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  hoverable }). perform ();  /examples/javascript/test/actionsApi/mouse/moveToElement.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( "assert" ); 
 describe ( 'Move to element' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'Mouse move into an element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  hoverable  =  driver . findElement ( By . id ( "hover" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ origin :  hoverable }). perform (); 
 
     const  status  =  await  driver . findElement ( By . id ( 'move-status' )). getText (); 
     assert . deepStrictEqual ( status ,  `hovered` ) 
   }); 
 }); 
                . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
通过偏移量移动动光标 这些方法让光标先移动到指定的坐标原点,然后通过单位为px的偏移量进行光标相对原点的偏移移动。
注意光标位置必须在可视窗口区域否则会报错。
从元素中心点(原点)偏移 这个方法是指先将光标移动到元素中心点(原点),然后通过偏移量进行光标相对原点的偏移。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  mouseTracker  =  driver . findElement ( By . id ( "mouse-tracker" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 ,  origin :  mouseTracker }). perform ();  /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js 
Copy
 
Close 
const  { By ,  Origin ,  Builder ,  until  }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Mouse move by offset' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'From element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  mouseTracker  =  driver . findElement ( By . id ( "mouse-tracker" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 ,  origin :  mouseTracker }). perform (); 
 
     await  driver . wait ( until . elementTextContains ( await  driver . findElement ( By . id ( 'relative-location' )),  "," ),  2000 ); 
     let  result  =  await  driver . findElement ( By . id ( 'relative-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From viewport origin' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 }). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From current pointer location' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  6 ,  y :  3 }). perform () 
 
     await  actions . move ({ x :  13 ,  y :  15 ,  origin :  Origin . POINTER }). perform () 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ])  -  6  -  13 )  <  2 ,  true ) 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ])  -  3  -  15 )  <  2 ,  true ) 
   }); 
 }); 
                . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " )  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
从视窗左上角(原点)偏移 这个方法是指先将光标移动到视窗左上角(原点),然后通过偏移量进行光标相对原点的偏移。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 }). perform ();  /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js 
Copy
 
Close 
const  { By ,  Origin ,  Builder ,  until  }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Mouse move by offset' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'From element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  mouseTracker  =  driver . findElement ( By . id ( "mouse-tracker" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 ,  origin :  mouseTracker }). perform (); 
 
     await  driver . wait ( until . elementTextContains ( await  driver . findElement ( By . id ( 'relative-location' )),  "," ),  2000 ); 
     let  result  =  await  driver . findElement ( By . id ( 'relative-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From viewport origin' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 }). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From current pointer location' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  6 ,  y :  3 }). perform () 
 
     await  actions . move ({ x :  13 ,  y :  15 ,  origin :  Origin . POINTER }). perform () 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ])  -  6  -  13 )  <  2 ,  true ) 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ])  -  3  -  15 )  <  2 ,  true ) 
   }); 
 }); 
        val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
从当前光标位置(原点)偏移 这个方法是指光标位于当前位置(原点),然后通过偏移量进行光标相对原点的偏移。
如果之前没有移动过光标位置,则这个位置是视窗左上角(原点)。
注意当页面发生滚动后光标位置不会发生变化。
注意第一个参数指定为正数时向右移动,第二个参数指定为正数时向下移动。所以 moveByOffset(30, -10) 是指从当前光标位置向右移动30个像素位置和向上移动10个像素位置。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    await  actions . move ({ x :  13 ,  y :  15 ,  origin :  Origin . POINTER }). perform ()  /examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js 
Copy
 
Close 
const  { By ,  Origin ,  Builder ,  until  }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Mouse move by offset' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'From element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  mouseTracker  =  driver . findElement ( By . id ( "mouse-tracker" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 ,  origin :  mouseTracker }). perform (); 
 
     await  driver . wait ( until . elementTextContains ( await  driver . findElement ( By . id ( 'relative-location' )),  "," ),  2000 ); 
     let  result  =  await  driver . findElement ( By . id ( 'relative-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From viewport origin' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  8 ,  y :  0 }). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual (( Math . abs ( parseInt ( result [ 0 ])  -  8 )  <  2 ),  true ) 
   }); 
 
   it ( 'From current pointer location' ,  async  function  ()  { 
     await  driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . move ({ x :  6 ,  y :  3 }). perform () 
 
     await  actions . move ({ x :  13 ,  y :  15 ,  origin :  Origin . POINTER }). perform () 
 
     let  result  =  await  driver . findElement ( By . id ( 'absolute-location' )). getText (); 
     result  =  result . split ( ', ' ); 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 0 ])  -  6  -  13 )  <  2 ,  true ) 
     assert . deepStrictEqual ( Math . abs ( parseInt ( result [ 1 ])  -  3  -  15 )  <  2 ,  true ) 
   }); 
 }); 
                . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )   /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
拖放元素 这个方法首先在原元素上提交执行按下鼠标左键,移动到目标元素位置后是释放鼠标左键。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     const  droppable  =  await  driver . findElement ( By . id ( "droppable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  droppable ). perform ();  /examples/javascript/test/actionsApi/mouse/dragAndDrop.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Drag and Drop' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'By Offset' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     let  start  =  await  draggable . getRect (); 
     let  finish  =  await  driver . findElement ( By . id ( "droppable" )). getRect (); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  { x :  finish . x  -  start . x ,  y :  finish . y  -  start . y }). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( "drop-status" )). getText (); 
     assert . deepStrictEqual ( 'dropped' ,  result ) 
   }); 
 
   it ( 'Onto Element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     const  droppable  =  await  driver . findElement ( By . id ( "droppable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  droppable ). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( "drop-status" )). getText (); 
     assert . deepStrictEqual ( 'dropped' ,  result ) 
   }); 
 }); 
        Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
通过偏移量拖放元素 这个方法首先在原元素上提交执行按下鼠标左键,通过给出的偏移量移动元素后释放鼠标左键。
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Collections ; 
 
 public   class  MouseTest   extends   BaseChromeTest   { 
      @Test 
      public   void   clickAndHold ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . clickAndHold ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "focused" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   clickAndRelease ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "click" )); 
          new   Actions ( driver ) 
                  . click ( clickable ) 
                  . perform (); 
 
          Assertions . assertTrue ( driver . getCurrentUrl (). contains ( "resultPage.html" )); 
      } 
 
      @Test 
      public   void   rightClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . contextClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "context-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   backClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          Assertions . assertEquals ( driver . getTitle (),   "We Arrive Here" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "BasicMouseInterfaceTest" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   forwardClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . findElement ( By . id ( "click" )). click (); 
          driver . navigate (). back (); 
          Assertions . assertEquals ( driver . getTitle (),   "BasicMouseInterfaceTest" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                  . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          Assertions . assertEquals ( "We Arrive Here" ,   driver . getTitle ()); 
      } 
 
      @Test 
      public   void   doubleClick ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   clickable   =   driver . findElement ( By . id ( "clickable" )); 
          new   Actions ( driver ) 
                  . doubleClick ( clickable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "double-clicked" ,   driver . findElement ( By . id ( "click-status" )). getText ()); 
      } 
 
      @Test 
      public   void   hovers ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   hoverable   =   driver . findElement ( By . id ( "hover" )); 
          new   Actions ( driver ) 
                  . moveToElement ( hoverable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "hovered" ,   driver . findElement ( By . id ( "move-status" )). getText ()); 
      } 
 
      @Test 
      public   void   moveByOffsetFromElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
          driver . manage (). window (). fullscreen (); 
 
          WebElement   tracker   =   driver . findElement ( By . id ( "mouse-tracker" )); 
          new   Actions ( driver ) 
                  . moveToElement ( tracker ,   8 ,   0 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   100   -   8 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromViewport ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   12 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   12 )   <   2 ); 
      } 
 
      @Test 
      public   void   moveByOffsetFromCurrentPointer ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          PointerInput   mouse   =   new   PointerInput ( PointerInput . Kind . MOUSE ,   "default mouse" ); 
 
          Sequence   actions   =   new   Sequence ( mouse ,   0 ) 
                  . addAction ( mouse . createPointerMove ( Duration . ZERO ,   PointerInput . Origin . viewport (),   8 ,   11 )); 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actions )); 
 
          new   Actions ( driver ) 
                  . moveByOffset ( 13 ,   15 ) 
                  . perform (); 
 
          String []   result   =   driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ] )   -   8   -   13 )   <   2 ); 
          Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ] )   -   11   -   15 )   <   2 ); 
      } 
 
      @Test 
      public   void   dragsToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          WebElement   droppable   =   driver . findElement ( By . id ( "droppable" )); 
          new   Actions ( driver ) 
                  . dragAndDrop ( draggable ,   droppable ) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 
      @Test 
      public   void   dragsByOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ); 
 
          WebElement   draggable   =   driver . findElement ( By . id ( "draggable" )); 
          Rectangle   start   =   draggable . getRect (); 
          Rectangle   finish   =   driver . findElement ( By . id ( "droppable" )). getRect (); 
          new   Actions ( driver ) 
                  . dragAndDropBy ( draggable ,   finish . getX ()   -   start . getX (),   finish . getY ()   -   start . getY ()) 
                  . perform (); 
 
          Assertions . assertEquals ( "dropped" ,   driver . findElement ( By . id ( "drop-status" )). getText ()); 
      } 
 } 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform ()  /examples/python/tests/actions_api/test_mouse.py 
Copy
 
Close 
import  pytest 
from  time  import  sleep 
from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.mouse_button  import  MouseButton 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.support.ui  import  WebDriverWait 
from  selenium.webdriver.support  import  expected_conditions  as  EC 
 
 def  test_click_and_hold ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . click_and_hold ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "focused" 
 
 
 def  test_click_and_release ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "click" ) 
     ActionChains ( driver )  \
         . click ( clickable )  \
         . perform () 
 
     assert  "resultPage.html"  in  driver . current_url 
 
 
 def  test_right_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . context_click ( clickable )  \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "context-clicked" 
 
 
 def  test_back_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     assert  driver . title  ==  "We Arrive Here" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . BACK ) 
     action . pointer_action . pointer_up ( MouseButton . BACK ) 
     action . perform () 
 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
 
 def  test_forward_click_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     driver . find_element ( By . ID ,  "click" ) . click () 
     driver . back () 
     assert  driver . title  ==  "BasicMouseInterfaceTest" 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . pointer_down ( MouseButton . FORWARD ) 
     action . pointer_action . pointer_up ( MouseButton . FORWARD ) 
     action . perform () 
 
     assert  driver . title  ==  "We Arrive Here" 
 
 
 def  test_double_click ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     clickable  =  driver . find_element ( By . ID ,  "clickable" ) 
     ActionChains ( driver )  \
         . double_click ( clickable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "click-status" ) . text  ==  "double-clicked" 
 
 
 def  test_hover ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     hoverable  =  driver . find_element ( By . ID ,  "hover" ) 
     ActionChains ( driver )  \
         . move_to_element ( hoverable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "move-status" ) . text  ==  "hovered" 
 
 
 def  test_move_by_offset_from_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     mouse_tracker  =  driver . find_element ( By . ID ,  "mouse-tracker" ) 
     ActionChains ( driver )  \
         . move_to_element_with_offset ( mouse_tracker ,  8 ,  0 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "relative-location" ) . text . split ( ", " ) 
     assert  abs ( int ( coordinates [ 0 ])  -  100  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_viewport_origin_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
     WebDriverWait ( driver ,  10 ) . until ( EC . presence_of_element_located (( By . ID ,  "absolute-location" ))) 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 8 ,  0 ) 
     action . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  8 )  <  2 
 
 
 def  test_move_by_offset_from_current_pointer_ab ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     action  =  ActionBuilder ( driver ) 
     action . pointer_action . move_to_location ( 6 ,  3 ) 
     action . perform () 
 
     ActionChains ( driver )  \
         . move_by_offset ( 13 ,  15 )  \
         . perform () 
 
     coordinates  =  driver . find_element ( By . ID ,  "absolute-location" ) . text . split ( ", " ) 
 
     assert  abs ( int ( coordinates [ 0 ])  -  6  -  13 )  <  2 
     assert  abs ( int ( coordinates [ 1 ])  -  3  -  15 )  <  2 
 
 
 def  test_drag_and_drop_onto_element ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     droppable  =  driver . find_element ( By . ID ,  "droppable" ) 
     ActionChains ( driver )  \
         . drag_and_drop ( draggable ,  droppable )  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
 
 
 def  test_drag_and_drop_by_offset ( driver ): 
    driver . get ( 'https://selenium.dev/selenium/web/mouse_interaction.html' ) 
 
     draggable  =  driver . find_element ( By . ID ,  "draggable" ) 
     start  =  draggable . location 
     finish  =  driver . find_element ( By . ID ,  "droppable" ) . location 
     ActionChains ( driver )  \
         . drag_and_drop_by_offset ( draggable ,  finish [ 'x' ]  -  start [ 'x' ],  finish [ 'y' ]  -  start [ 'y' ])  \
         . perform () 
 
     assert  driver . find_element ( By . ID ,  "drop-status" ) . text  ==  "dropped" 
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs 
Copy
 
Close 
using  System ; 
using  System.Drawing ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  MouseTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ClickAndHold () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ClickAndHold ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "focused" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  ClickAndRelease () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "click" )); 
             new  Actions ( driver ) 
                 . Click ( clickable ) 
                 . Perform (); 
             
             Assert . IsTrue ( driver . Url . Contains ( "resultPage.html" )); 
         } 
 
         [TestMethod] 
        public  void  RightClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . ContextClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "context-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
         
         [TestMethod] 
        public  void  BackClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Back )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Back )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  ForwardClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             driver . FindElement ( By . Id ( "click" )). Click (); 
             driver . Navigate (). Back (); 
             Assert . AreEqual ( "BasicMouseInterfaceTest" ,  driver . Title ); 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerDown ( MouseButton . Forward )); 
             actionBuilder . AddAction ( mouse . CreatePointerUp ( MouseButton . Forward )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             Assert . AreEqual ( "We Arrive Here" ,  driver . Title ); 
         } 
 
         [TestMethod] 
        public  void  DoubleClick () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  clickable  =  driver . FindElement ( By . Id ( "clickable" )); 
             new  Actions ( driver ) 
                 . DoubleClick ( clickable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "double-clicked" ,  driver . FindElement ( By . Id ( "click-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  Hovers () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  hoverable  =  driver . FindElement ( By . Id ( "hover" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( hoverable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "hovered" ,  driver . FindElement ( By . Id ( "move-status" )). Text ); 
         } 
 
         [TestMethod] 
        [Obsolete("Obsolete")] 
        public  void  MoveByOffsetFromTopLeftOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCenterOfElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  tracker  =  driver . FindElement ( By . Id ( "mouse-tracker" )); 
             new  Actions ( driver ) 
                 . MoveToElement ( tracker ,  8 ,  0 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "relative-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  100  -  8 )  <  2 ); 
         } 
         
         [TestMethod] 
        public  void  MoveByOffsetFromViewport () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  0 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  MoveByOffsetFromCurrentPointerLocation () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  mouse  =  new  PointerInputDevice ( PointerKind . Mouse ,  "default mouse" ); 
             actionBuilder . AddAction ( mouse . CreatePointerMove ( CoordinateOrigin . Viewport , 
                 8 ,  12 ,  TimeSpan . Zero )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             new  Actions ( driver ) 
                 . MoveByOffset ( 13 ,  15 ) 
                 . Perform (); 
 
             string []  result  =  driver . FindElement ( By . Id ( "absolute-location" )). Text . Split ( ", " ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 0 ])  -  8  -  13 )  <  2 ); 
             Assert . IsTrue ( Math . Abs ( int . Parse ( result [ 1 ])  -  12  -  15 )  <  2 ); 
         } 
 
         [TestMethod] 
        public  void  DragToElement () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             IWebElement  droppable  =  driver . FindElement ( By . Id ( "droppable" )); 
             new  Actions ( driver ) 
                 . DragAndDrop ( draggable ,  droppable ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
 
         [TestMethod] 
        public  void  DragByOffset () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/mouse_interaction.html" ; 
             
             IWebElement  draggable  =  driver . FindElement ( By . Id ( "draggable" )); 
             Point  start  =  draggable . Location ; 
             Point  finish  =  driver . FindElement ( By . Id ( "droppable" )). Location ; 
             new  Actions ( driver ) 
                 . DragAndDropToOffset ( draggable ,  finish . X  -  start . X ,  finish . Y  -  start . Y ) 
                 . Perform (); 
             
             Assert . AreEqual ( "dropped" ,  driver . FindElement ( By . Id ( "drop-status" )). Text ); 
         } 
     } 
 }     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform  /examples/ruby/spec/actions_api/mouse_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Mouse'  do 
  let ( :driver )  {  start_session  } 
 
   it  'click and hold'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . click_and_hold ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'focused' 
   end 
 
   it  'click and release'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'click' ) 
     driver . action 
           . click ( clickable ) 
           . perform 
 
     expect ( driver . current_url ) . to  include  'resultPage.html' 
   end 
 
   describe  'alternate button clicks'  do 
     it  'right click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       clickable  =  driver . find_element ( id :  'clickable' ) 
       driver . action 
             . context_click ( clickable ) 
             . perform 
 
       expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'context-clicked' 
     end 
 
     it  'back click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
 
       driver . action 
             . pointer_down ( :back ) 
             . pointer_up ( :back ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
     end 
 
     it  'forward click'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . find_element ( id :  'click' ) . click 
       driver . navigate . back 
       expect ( driver . title ) . to  eq ( 'BasicMouseInterfaceTest' ) 
 
       driver . action 
             . pointer_down ( :forward ) 
             . pointer_up ( :forward ) 
             . perform 
 
       expect ( driver . title ) . to  eq ( 'We Arrive Here' ) 
     end 
   end 
 
   it  'double click'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     clickable  =  driver . find_element ( id :  'clickable' ) 
     driver . action 
           . double_click ( clickable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'click-status' ) . text ) . to  eq  'double-clicked' 
   end 
 
   it  'hovers'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     hoverable  =  driver . find_element ( id :  'hover' ) 
     driver . action 
           . move_to ( hoverable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'move-status' ) . text ) . to  eq  'hovered' 
   end 
 
   describe  'move by offset'  do 
     it  'offset from element'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
       driver . action . scroll_to ( driver . find_element ( id :  'bottom' )) . perform 
 
       mouse_tracker  =  driver . find_element ( id :  'mouse-tracker' ) 
       driver . action 
             . move_to ( mouse_tracker ,  8 ,  11 ) 
             . perform 
 
       rect  =  mouse_tracker . rect 
       center_x  =  rect . width  /  2 
       center_y  =  rect . height  /  2 
       x_coord ,  y_coord  =  driver . find_element ( id :  'relative-location' ) . text . split ( ',' ) . map ( & :to_i ) 
 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( center_x  +  8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( center_y  +  11 ) 
     end 
 
     it  'offset from viewport'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action 
             . move_to_location ( 8 ,  12 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 12 ) 
     end 
 
     it  'offset from current pointer location'  do 
       driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
       driver . action . move_to_location ( 8 ,  11 ) . perform 
 
       driver . action 
             . move_by ( 13 ,  15 ) 
             . perform 
 
       x_coord ,  y_coord  =  driver . find_element ( id :  'absolute-location' ) . text . split ( ',' ) . map ( & :to_i ) 
       expect ( x_coord ) . to  be_within ( 1 ) . of ( 8  +  13 ) 
       expect ( y_coord ) . to  be_within ( 1 ) . of ( 11  +  15 ) 
     end 
   end 
 
   it  'drags to another element'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     droppable  =  driver . find_element ( id :  'droppable' ) 
     driver . action 
           . drag_and_drop ( draggable ,  droppable ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 
   it  'drags by an offset'  do 
     driver . get  'https://www.selenium.dev/selenium/web/mouse_interaction.html' 
 
     draggable  =  driver . find_element ( id :  'draggable' ) 
     start  =  draggable . rect 
     finish  =  driver . find_element ( id :  'droppable' ) . rect 
     driver . action 
           . drag_and_drop_by ( draggable ,  finish . x  -  start . x ,  finish . y  -  start . y ) 
           . perform 
 
     expect ( driver . find_element ( id :  'drop-status' ) . text ) . to  include ( 'dropped' ) 
   end 
 end 
    const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     let  start  =  await  draggable . getRect (); 
     let  finish  =  await  driver . findElement ( By . id ( "droppable" )). getRect (); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  { x :  finish . x  -  start . x ,  y :  finish . y  -  start . y }). perform ();  /examples/javascript/test/actionsApi/mouse/dragAndDrop.spec.js 
Copy
 
Close 
const  { By ,  Builder }  =  require ( 'selenium-webdriver' ); 
const  assert  =  require ( 'assert' ); 
 describe ( 'Drag and Drop' ,  function  ()  { 
  let  driver ; 
 
   before ( async  function  ()  { 
     driver  =  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }); 
 
   after ( async  ()  =>  await  driver . quit ()); 
 
   it ( 'By Offset' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     let  start  =  await  draggable . getRect (); 
     let  finish  =  await  driver . findElement ( By . id ( "droppable" )). getRect (); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  { x :  finish . x  -  start . x ,  y :  finish . y  -  start . y }). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( "drop-status" )). getText (); 
     assert . deepStrictEqual ( 'dropped' ,  result ) 
   }); 
 
   it ( 'Onto Element' ,  async  function  ()  { 
     await  driver . get ( 'https://www.selenium.dev/selenium/web/mouse_interaction.html' ); 
     const  draggable  =  driver . findElement ( By . id ( "draggable" )); 
     const  droppable  =  await  driver . findElement ( By . id ( "droppable" )); 
     const  actions  =  driver . actions ({ async :  true }); 
     await  actions . dragAndDrop ( draggable ,  droppable ). perform (); 
 
     let  result  =  await  driver . findElement ( By . id ( "drop-status" )). getText (); 
     assert . deepStrictEqual ( 'dropped' ,  result ) 
   }); 
 }); 
        val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ())  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  java.time.Duration 
import  java.util.Collections 
 class  MouseTest  :  BaseTest ()  { 
        
     @Test 
     fun  clickAndHold ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . clickAndHold ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "focused" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     }  
 
     @Test 
     fun  clickAndRelease ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "click" )) 
         Actions ( driver ) 
                 . click ( clickable ) 
                 . perform () 
 
         Assertions . assertTrue ( driver . getCurrentUrl () !! . contains ( "resultPage.html" )) 
     } 
   
     @Test 
     fun  rightClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . contextClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "context-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
       
     @Test 
     fun  backClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         Assertions . assertEquals ( driver . getTitle (),  "We Arrive Here" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . BACK . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . BACK . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "BasicMouseInterfaceTest" ,  driver . getTitle ()) 
     } 
      
     @Test 
     fun  forwardClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . findElement ( By . id ( "click" )). click () 
         driver . navigate (). back () 
         Assertions . assertEquals ( driver . getTitle (),  "BasicMouseInterfaceTest" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerDown ( PointerInput . MouseButton . FORWARD . asArg ())) 
                 . addAction ( mouse . createPointerUp ( PointerInput . MouseButton . FORWARD . asArg ())) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Assertions . assertEquals ( "We Arrive Here" ,  driver . getTitle ()) 
     } 
  
     @Test 
     fun  doubleClick ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  clickable  =  driver . findElement ( By . id ( "clickable" )) 
         Actions ( driver ) 
                 . doubleClick ( clickable ) 
                 . perform () 
 
         Assertions . assertEquals ( "double-clicked" ,  driver . findElement ( By . id ( "click-status" )). getText ()) 
     } 
 
     @Test 
     fun  hovers ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  hoverable  =  driver . findElement ( By . id ( "hover" )) 
         Actions ( driver ) 
                 . moveToElement ( hoverable ) 
                 . perform () 
 
         Assertions . assertEquals ( "hovered" ,  driver . findElement ( By . id ( "move-status" )). getText ()) 
     } 
 
     @Test 
     fun  moveByOffsetFromElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
         driver . manage (). window (). fullscreen () 
 
         val  tracker  =  driver . findElement ( By . id ( "mouse-tracker" )) 
         Actions ( driver ) 
                 . moveToElement ( tracker ,  8 ,  0 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "relative-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  100  -  8 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromViewport ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  12 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  12 )  <  2 ) 
     } 
 
     @Test 
     fun  moveByOffsetFromCurrentPointer ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  mouse  =  PointerInput ( PointerInput . Kind . MOUSE ,  "default mouse" ) 
 
         val  actions  =  Sequence ( mouse ,  0 ) 
                 . addAction ( mouse . createPointerMove ( Duration . ZERO ,  PointerInput . Origin . viewport (),  8 ,  11 )) 
         ( driver  as  RemoteWebDriver ). perform ( Collections . singletonList ( actions )) 
 
         Actions ( driver ) 
                 . moveByOffset ( 13 ,  15 ) 
                 . perform () 
 
         val  result  =  driver . findElement ( By . id ( "absolute-location" )). getText (). split ( ", " )  
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 0 ])  -  8  -  13 )  <  2 ) 
         Assertions . assertTrue ( Math . abs ( Integer . parseInt ( result [ 1 ])  -  11  -  15 )  <  2 ) 
     } 
     
     @Test 
     fun  dragsToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  droppable  =  driver . findElement ( By . id ( "droppable" )) 
         Actions ( driver ) 
                 . dragAndDrop ( draggable ,  droppable ) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
         
     @Test 
     fun  dragsByOffset ()  {  
         driver . get ( "https://www.selenium.dev/selenium/web/mouse_interaction.html" ) 
 
         val  draggable  =  driver . findElement ( By . id ( "draggable" )) 
         val  start  =  draggable . getRect () 
         val  finish  =  driver . findElement ( By . id ( "droppable" )). getRect () 
         Actions ( driver ) 
                 . dragAndDropBy ( draggable ,  finish . getX ()  -  start . getX (),  finish . getY ()  -  start . getY ()) 
                 . perform () 
 
         Assertions . assertEquals ( "dropped" ,  driver . findElement ( By . id ( "drop-status" )). getText ()) 
     } 
 } 
3 - 触控笔操作 一种用于与网页交互的类似笔尖的指针输入设备的表示.
Chromium Only 
触控笔是一种指针输入设备,其行为大多与鼠标相同,
但也可以具有触控笔特有的事件属性。
此外,鼠标有 5 个按钮,而触控笔有 3 种等效的按钮状态:
0 - 触摸接触(默认设置;相当于左键单击) 2 - 桶形按钮/侧键(相当于右键点击) 5 - 橡皮擦按钮(当前驱动程序不支持) 使用触控笔 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin Selenium v4.2 
         WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          new   Actions ( driver ) 
                  . setActivePointer ( PointerInput . Kind . PEN ,   "default pen" ) 
                  . moveToElement ( pointerArea ) 
                  . clickAndHold () 
                  . moveByOffset ( 2 ,   2 ) 
                  . release () 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/PenTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Arrays ; 
 import   java.util.Collections ; 
 import   java.util.List ; 
 import   java.util.Map ; 
 import   java.util.stream.Collectors ; 
 
 public   class  PenTest   extends   BaseChromeTest   { 
      @Test 
      public   void   usePen ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/pointerActionsPage.html" ); 
 
          WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          new   Actions ( driver ) 
                  . setActivePointer ( PointerInput . Kind . PEN ,   "default pen" ) 
                  . moveToElement ( pointerArea ) 
                  . clickAndHold () 
                  . moveByOffset ( 2 ,   2 ) 
                  . release () 
                  . perform (); 
 
          List < WebElement >   moves   =   driver . findElements ( By . className ( "pointermove" )); 
          Map < String ,   String >   moveTo   =   getPropertyInfo ( moves . get ( 0 )); 
          Map < String ,   String >   down   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))); 
          Map < String ,   String >   moveBy   =   getPropertyInfo ( moves . get ( 1 )); 
          Map < String ,   String >   up   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))); 
 
          Rectangle   rect   =   pointerArea . getRect (); 
 
          int   centerX   =   ( int )   Math . floor ( rect . width   /   2   +   rect . getX ()); 
          int   centerY   =   ( int )   Math . floor ( rect . height   /   2   +   rect . getY ()); 
          Assertions . assertEquals ( "-1" ,   moveTo . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveTo . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   moveTo . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   moveTo . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   down . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   down . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   down . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   down . get ( "pageY" )); 
          Assertions . assertEquals ( "-1" ,   moveBy . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveBy . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   moveBy . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   moveBy . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   up . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   up . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   up . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   up . get ( "pageY" )); 
      } 
 
      @Test 
      public   void   setPointerEventProperties ()   { 
          driver . get ( "https://selenium.dev/selenium/web/pointerActionsPage.html" ); 
 
          WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          PointerInput   pen   =   new   PointerInput ( PointerInput . Kind . PEN ,   "default pen" ); 
          PointerInput . PointerEventProperties   eventProperties   =   PointerInput . eventProperties () 
                  . setTiltX ( - 72 ) 
                  . setTiltY ( 9 ) 
                  . setTwist ( 86 ); 
          PointerInput . Origin   origin   =   PointerInput . Origin . fromElement ( pointerArea ); 
 
          Sequence   actionListPen   =   new   Sequence ( pen ,   0 ) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   0 ,   0 )) 
                  . addAction ( pen . createPointerDown ( 0 )) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   2 ,   2 ,   eventProperties )) 
                  . addAction ( pen . createPointerUp ( 0 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actionListPen )); 
 
          List < WebElement >   moves   =   driver . findElements ( By . className ( "pointermove" )); 
          Map < String ,   String >   moveTo   =   getPropertyInfo ( moves . get ( 0 )); 
          Map < String ,   String >   down   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))); 
          Map < String ,   String >   moveBy   =   getPropertyInfo ( moves . get ( 1 )); 
          Map < String ,   String >   up   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))); 
 
          Rectangle   rect   =   pointerArea . getRect (); 
          int   centerX   =   ( int )   Math . floor ( rect . width   /   2   +   rect . getX ()); 
          int   centerY   =   ( int )   Math . floor ( rect . height   /   2   +   rect . getY ()); 
          Assertions . assertEquals ( "-1" ,   moveTo . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveTo . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   moveTo . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   moveTo . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   down . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   down . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   down . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   down . get ( "pageY" )); 
          Assertions . assertEquals ( "-1" ,   moveBy . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveBy . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   moveBy . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   moveBy . get ( "pageY" )); 
          Assertions . assertEquals ( "-72" ,   moveBy . get ( "tiltX" )); 
          Assertions . assertEquals ( "9" ,   moveBy . get ( "tiltY" )); 
          Assertions . assertEquals ( "86" ,   moveBy . get ( "twist" )); 
          Assertions . assertEquals ( "0" ,   up . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   up . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   up . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   up . get ( "pageY" )); 
      } 
 
      private   Map < String ,   String >   getPropertyInfo ( WebElement   element )   { 
          String   text   =   element . getText (); 
          text   =   text . substring ( text . indexOf ( ' ' )   +   1 ); 
 
          return   Arrays . stream ( text . split ( ", " )) 
                  . map ( s   ->   s . split ( ": " )) 
                  . collect ( Collectors . toMap ( 
                          a   ->   a [ 0 ] , 
                          a   ->   a [ 1 ] 
                  )); 
      } 
 } 
 Selenium v4.2 
    pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ) \
         . pointer_up () 
     action . perform ()  /examples/python/tests/actions_api/test_pen.py 
Copy
 
Close 
import  math 
 from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.interaction  import  POINTER_PEN 
from  selenium.webdriver.common.actions.pointer_input  import  PointerInput 
from  selenium.webdriver.common.by  import  By 
 
 def  test_use_pen ( driver ): 
    driver . get ( 'https://www.selenium.dev/selenium/web/pointerActionsPage.html' ) 
 
     pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ) \
         . pointer_up () 
     action . perform () 
 
     moves  =  driver . find_elements ( By . CLASS_NAME ,  "pointermove" ) 
     move_to  =  properties ( moves [ 0 ]) 
     down  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerdown" )) 
     move_by  =  properties ( moves [ 1 ]) 
     up  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerup" )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect [ "x" ]  +  rect [ "width" ]  /  2 
     center_y  =  rect [ "y" ]  +  rect [ "height" ]  /  2 
 
     assert  move_to [ "button" ]  ==  "-1" 
     assert  move_to [ "pointerType" ]  ==  "pen" 
     assert  move_to [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  move_to [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  down [ "button" ]  ==  "0" 
     assert  down [ "pointerType" ]  ==  "pen" 
     assert  down [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  down [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  move_by [ "button" ]  ==  "-1" 
     assert  move_by [ "pointerType" ]  ==  "pen" 
     assert  move_by [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  move_by [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
     assert  up [ "button" ]  ==  "0" 
     assert  up [ "pointerType" ]  ==  "pen" 
     assert  up [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  up [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
 
 
 def  test_set_pointer_event_properties ( driver ): 
    driver . get ( 'https://www.selenium.dev/selenium/web/pointerActionsPage.html' ) 
 
     pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ,  tilt_x =- 72 ,  tilt_y = 9 ,  twist = 86 ) \
         . pointer_up ( 0 ) 
     action . perform () 
 
     moves  =  driver . find_elements ( By . CLASS_NAME ,  "pointermove" ) 
     move_to  =  properties ( moves [ 0 ]) 
     down  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerdown" )) 
     move_by  =  properties ( moves [ 1 ]) 
     up  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerup" )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect [ "x" ]  +  rect [ "width" ]  /  2 
     center_y  =  rect [ "y" ]  +  rect [ "height" ]  /  2 
 
     assert  move_to [ "button" ]  ==  "-1" 
     assert  move_to [ "pointerType" ]  ==  "pen" 
     assert  move_to [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  move_to [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  down [ "button" ]  ==  "0" 
     assert  down [ "pointerType" ]  ==  "pen" 
     assert  down [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  down [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  move_by [ "button" ]  ==  "-1" 
     assert  move_by [ "pointerType" ]  ==  "pen" 
     assert  move_by [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  move_by [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
     assert  move_by [ "tiltX" ]  ==  "-72" 
     assert  move_by [ "tiltY" ]  ==  "9" 
     assert  move_by [ "twist" ]  ==  "86" 
     assert  up [ "button" ]  ==  "0" 
     assert  up [ "pointerType" ]  ==  "pen" 
     assert  up [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  up [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
 
 
 def  properties ( element ): 
    kv  =  element . text . split ( ' ' ,  1 )[ 1 ] . split ( ', ' ) 
     return  { x [ 0 ]: x [ 1 ]  for  x  in  list ( map ( lambda  item :  item . split ( ': ' ),  kv ))} 
 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());  /examples/dotnet/SeleniumDocs/ActionsAPI/PenTest.cs 
Copy
 
Close 
using  System ; 
using  System.Collections.Generic ; 
using  System.Drawing ; 
using  System.Linq ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  PenTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  UsePen () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/pointerActionsPage.html" ; 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             var  moves  =  driver . FindElements ( By . ClassName ( "pointermove" )); 
             var  moveTo  =  getProperties ( moves . ElementAt ( 0 )); 
             var  down  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerdown" ))); 
             var  moveBy  =  getProperties ( moves . ElementAt ( 1 )); 
             var  up  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerup" ))); 
 
             Point  location  =  pointerArea . Location ; 
             Size  size  =  pointerArea . Size ; 
             decimal  centerX  =  location . X  +  size . Width  /  2 ; 
             decimal  centerY  =  location . Y  +  size . Height  /  2 ; 
 
             Assert . AreEqual ( "-1" ,  moveTo [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveTo [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  moveTo [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  moveTo [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  down [ "button" ]); 
             Assert . AreEqual ( "pen" ,  down [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  down [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  down [ "pageY" ])); 
             Assert . AreEqual ( "-1" ,  moveBy [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveBy [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  moveBy [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  moveBy [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  up [ "button" ]); 
             Assert . AreEqual ( "pen" ,  up [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  up [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  up [ "pageY" ])); 
         } 
 
         [TestMethod] 
        public  void  SetPointerEventProperties () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/pointerActionsPage.html" ; 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             PointerInputDevice . PointerEventProperties  properties  =  new  PointerInputDevice . PointerEventProperties ()  { 
                 TiltX  =  - 72 , 
                 TiltY  =  9 , 
                 Twist  =  86 , 
             };             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero ,  properties )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             var  moves  =  driver . FindElements ( By . ClassName ( "pointermove" )); 
             var  moveTo  =  getProperties ( moves . ElementAt ( 0 )); 
             var  down  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerdown" ))); 
             var  moveBy  =  getProperties ( moves . ElementAt ( 1 )); 
             var  up  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerup" ))); 
 
             Point  location  =  pointerArea . Location ; 
             Size  size  =  pointerArea . Size ; 
             decimal  centerX  =  location . X  +  size . Width  /  2 ; 
             decimal  centerY  =  location . Y  +  size . Height  /  2 ; 
 
             Assert . AreEqual ( "-1" ,  moveTo [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveTo [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  moveTo [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  moveTo [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  down [ "button" ]); 
             Assert . AreEqual ( "pen" ,  down [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  down [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  down [ "pageY" ])); 
             Assert . AreEqual ( "-1" ,  moveBy [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveBy [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  moveBy [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  moveBy [ "pageY" ])); 
             Assert . AreEqual ((- 72 ). ToString (),  moveBy [ "tiltX" ]); 
             Assert . AreEqual (( 9 ). ToString (),  moveBy [ "tiltY" ]); 
             Assert . AreEqual (( 86 ). ToString (),  moveBy [ "twist" ]); 
             Assert . AreEqual ( "0" ,  up [ "button" ]); 
             Assert . AreEqual ( "pen" ,  up [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  up [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  up [ "pageY" ])); 
         } 
 
         private  Dictionary < string ,  string >  getProperties ( IWebElement  element ) 
         { 
             var  str  =  element . Text ; 
             str  =  str [( str . Split ()[ 0 ]. Length  +  1 )..]; 
             IEnumerable < string []>  keyValue  =  str . Split ( ", " ). Select ( part  =>  part . Split ( ":" )); 
             return  keyValue . ToDictionary ( split  =>  split [ 0 ]. Trim (),  split  =>  split [ 1 ]. Trim ()); 
         } 
 
         private  bool  VerifyEquivalent ( decimal  expected ,  string  actual ) 
         { 
             var  absolute  =  Math . Abs ( expected  -  decimal . Parse ( actual )); 
             if  ( absolute  <=  1 ) 
             { 
                 return  true ; 
             } 
 
             throw  new  Exception ( "Expected: "  +  expected  +  "; but received: "  +  actual ); 
         } 
     } 
 } Selenium v4.2 
    pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ) 
           . pointer_up 
           . perform  /examples/ruby/spec/actions_api/pen_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Pen'  do 
  let ( :driver )  {  start_session  } 
 
   it  'uses a pen'  do 
     driver . get  'https://www.selenium.dev/selenium/web/pointerActionsPage.html' 
 
     pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ) 
           . pointer_up 
           . perform 
 
     moves  =  driver . find_elements ( class :  'pointermove' ) 
     move_to  =  properties ( moves [ 0 ] ) 
     down  =  properties ( driver . find_element ( class :  'pointerdown' )) 
     move_by  =  properties ( moves [ 1 ] ) 
     up  =  properties ( driver . find_element ( class :  'pointerup' )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect . x  +  ( rect . width  /  2 ) 
     center_y  =  rect . y  +  ( rect . height  /  2 ) 
 
     expect ( move_to ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  center_x . to_s , 
                                'pageY'  =>  center_y . floor . to_s ) 
     expect ( down ) . to  include ( 'button'  =>  '0' , 
                             'pointerType'  =>  'pen' , 
                             'pageX'  =>  center_x . to_s , 
                             'pageY'  =>  center_y . floor . to_s ) 
     expect ( move_by ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  ( center_x  +  2 ) . to_s , 
                                'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
     expect ( up ) . to  include ( 'button'  =>  '0' , 
                           'pointerType'  =>  'pen' , 
                           'pageX'  =>  ( center_x  +  2 ) . to_s , 
                           'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
   end 
 
   it  'sets pointer event attributes'  do 
     driver . get  'https://www.selenium.dev/selenium/web/pointerActionsPage.html' 
 
     pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ,  tilt_x :  - 72 ,  tilt_y :  9 ,  twist :  86 ) 
           . pointer_up 
           . perform 
 
     moves  =  driver . find_elements ( class :  'pointermove' ) 
     move_to  =  properties ( moves [ 0 ] ) 
     down  =  properties ( driver . find_element ( class :  'pointerdown' )) 
     move_by  =  properties ( moves [ 1 ] ) 
     up  =  properties ( driver . find_element ( class :  'pointerup' )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect . x  +  ( rect . width  /  2 ) 
     center_y  =  rect . y  +  ( rect . height  /  2 ) 
 
     expect ( move_to ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  center_x . to_s , 
                                'pageY'  =>  center_y . floor . to_s ) 
     expect ( down ) . to  include ( 'button'  =>  '0' , 
                             'pointerType'  =>  'pen' , 
                             'pageX'  =>  center_x . to_s , 
                             'pageY'  =>  center_y . floor . to_s ) 
     expect ( move_by ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  ( center_x  +  2 ) . to_s , 
                                'pageY'  =>  ( center_y  +  2 ) . floor . to_s , 
                                'tiltX'  =>  - 72 . to_s , 
                                'tiltY'  =>  9 . to_s , 
                                'twist'  =>  86 . to_s ) 
     expect ( up ) . to  include ( 'button'  =>  '0' , 
                           'pointerType'  =>  'pen' , 
                           'pageX'  =>  ( center_x  +  2 ) . to_s , 
                           'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
   end 
 
   def  properties ( element ) 
     element . text . sub ( /.*?\s/ ,  '' ) . split ( ',' ) . to_h  {  | item |  item . lstrip . split ( /\s*:\s*/ )  } 
   end 
 end 
        Actions ( driver ) 
                 . setActivePointer ( PointerInput . Kind . PEN ,  "default pen" ) 
                 . moveToElement ( pointerArea ) 
                 . clickAndHold () 
                 . moveByOffset ( 2 ,  2 ) 
                 . release () 
                 . perform () 
 
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/PenTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  kotlin.collections.Map 
import  java.time.Duration 
 class  PenTest  :  BaseTest ()  { 
  
     @Test 
     fun  usePen ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/pointerActionsPage.html" ) 
         
         val  pointerArea  =  driver . findElement ( By . id ( "pointerArea" )) 
         Actions ( driver ) 
                 . setActivePointer ( PointerInput . Kind . PEN ,  "default pen" ) 
                 . moveToElement ( pointerArea ) 
                 . clickAndHold () 
                 . moveByOffset ( 2 ,  2 ) 
                 . release () 
                 . perform () 
 
         val  moves  =  driver . findElements ( By . className ( "pointermove" )) 
         val  moveTo  =  getPropertyInfo ( moves . get ( 0 )) 
         val  down  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))) 
         val  moveBy  =  getPropertyInfo ( moves . get ( 1 )) 
         val  up  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))) 
         
         val  rect  =  pointerArea . getRect () 
 
         val  centerX  =  Math . floor ( rect . width . toDouble ()  /  2  +  rect . getX ()). toInt () 
         val  centerY  =  Math . floor ( rect . height . toDouble ()  /  2  +  rect . getY ()). toInt () 
         Assertions . assertEquals ( "-1" ,  moveTo . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveTo . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  moveTo . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  moveTo . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  down . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  down . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  down . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  down . get ( "pageY" )) 
         Assertions . assertEquals ( "-1" ,  moveBy . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveBy . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  moveBy . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  moveBy . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  up . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  up . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  up . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  up . get ( "pageY" )) 
     } 
 
     @Test 
     fun  setPointerEventProperties ()  { 
         driver . get ( "https://selenium.dev/selenium/web/pointerActionsPage.html" ) 
         
         val  pointerArea  =  driver . findElement ( By . id ( "pointerArea" )) 
         val  pen  =  PointerInput ( PointerInput . Kind . PEN ,  "default pen" ) 
         val  eventProperties  =  PointerInput . eventProperties () 
                 . setTiltX (- 72 ) 
                 . setTiltY ( 9 ) 
                 . setTwist ( 86 ) 
         val  origin  =  PointerInput . Origin . fromElement ( pointerArea ) 
         
         val  actionListPen  =  Sequence ( pen ,  0 ) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  0 ,  0 )) 
                 . addAction ( pen . createPointerDown ( 0 )) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  2 ,  2 ,  eventProperties )) 
                 . addAction ( pen . createPointerUp ( 0 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( listOf ( actionListPen )) 
                     
 
         val  moves  =  driver . findElements ( By . className ( "pointermove" )) 
         val  moveTo  =  getPropertyInfo ( moves . get ( 0 )) 
         val  down  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))) 
         val  moveBy  =  getPropertyInfo ( moves . get ( 1 )) 
         val  up  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))) 
         
         val  rect  =  pointerArea . getRect () 
 
         val  centerX  =  Math . floor ( rect . width . toDouble ()  /  2  +  rect . getX ()). toInt () 
         val  centerY  =  Math . floor ( rect . height . toDouble ()  /  2  +  rect . getY ()). toInt () 
         Assertions . assertEquals ( "-1" ,  moveTo . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveTo . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  moveTo . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  moveTo . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  down . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  down . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  down . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  down . get ( "pageY" )) 
         Assertions . assertEquals ( "-1" ,  moveBy . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveBy . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  moveBy . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  moveBy . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  up . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  up . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  up . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  up . get ( "pageY" ))  
 	    
         Assertions . assertEquals ( "-72" ,  moveBy . get ( "tiltX" )) 
         Assertions . assertEquals ( "9" ,  moveBy . get ( "tiltY" )) 
         Assertions . assertEquals ( "86" ,  moveBy . get ( "twist" )) 
     } 
     
     fun  getPropertyInfo ( element :  WebElement ):  Map < String ,  String >  { 
         var  text  =  element . getText ()   
         text  =  text . substring ( text . indexOf ( " " )+ 1 ) 
         return  text . split ( ", " ) 
         . map  {  it . split ( ": " )  } 
         . map  {  it . first ()  to  it . last ()  } 
         . toMap ()  
     } 
 } 添加指针事件属性 Selenium v4.2 
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          PointerInput   pen   =   new   PointerInput ( PointerInput . Kind . PEN ,   "default pen" ); 
          PointerInput . PointerEventProperties   eventProperties   =   PointerInput . eventProperties () 
                  . setTiltX ( - 72 ) 
                  . setTiltY ( 9 ) 
                  . setTwist ( 86 ); 
          PointerInput . Origin   origin   =   PointerInput . Origin . fromElement ( pointerArea ); 
 
          Sequence   actionListPen   =   new   Sequence ( pen ,   0 ) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   0 ,   0 )) 
                  . addAction ( pen . createPointerDown ( 0 )) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   2 ,   2 ,   eventProperties )) 
                  . addAction ( pen . createPointerUp ( 0 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actionListPen )); /examples/java/src/test/java/dev/selenium/actions_api/PenTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.Rectangle ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.PointerInput ; 
 import   org.openqa.selenium.interactions.Sequence ; 
 import   org.openqa.selenium.remote.RemoteWebDriver ; 
 
 import   java.time.Duration ; 
 import   java.util.Arrays ; 
 import   java.util.Collections ; 
 import   java.util.List ; 
 import   java.util.Map ; 
 import   java.util.stream.Collectors ; 
 
 public   class  PenTest   extends   BaseChromeTest   { 
      @Test 
      public   void   usePen ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/pointerActionsPage.html" ); 
 
          WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          new   Actions ( driver ) 
                  . setActivePointer ( PointerInput . Kind . PEN ,   "default pen" ) 
                  . moveToElement ( pointerArea ) 
                  . clickAndHold () 
                  . moveByOffset ( 2 ,   2 ) 
                  . release () 
                  . perform (); 
 
          List < WebElement >   moves   =   driver . findElements ( By . className ( "pointermove" )); 
          Map < String ,   String >   moveTo   =   getPropertyInfo ( moves . get ( 0 )); 
          Map < String ,   String >   down   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))); 
          Map < String ,   String >   moveBy   =   getPropertyInfo ( moves . get ( 1 )); 
          Map < String ,   String >   up   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))); 
 
          Rectangle   rect   =   pointerArea . getRect (); 
 
          int   centerX   =   ( int )   Math . floor ( rect . width   /   2   +   rect . getX ()); 
          int   centerY   =   ( int )   Math . floor ( rect . height   /   2   +   rect . getY ()); 
          Assertions . assertEquals ( "-1" ,   moveTo . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveTo . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   moveTo . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   moveTo . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   down . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   down . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   down . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   down . get ( "pageY" )); 
          Assertions . assertEquals ( "-1" ,   moveBy . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveBy . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   moveBy . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   moveBy . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   up . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   up . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   up . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   up . get ( "pageY" )); 
      } 
 
      @Test 
      public   void   setPointerEventProperties ()   { 
          driver . get ( "https://selenium.dev/selenium/web/pointerActionsPage.html" ); 
 
          WebElement   pointerArea   =   driver . findElement ( By . id ( "pointerArea" )); 
          PointerInput   pen   =   new   PointerInput ( PointerInput . Kind . PEN ,   "default pen" ); 
          PointerInput . PointerEventProperties   eventProperties   =   PointerInput . eventProperties () 
                  . setTiltX ( - 72 ) 
                  . setTiltY ( 9 ) 
                  . setTwist ( 86 ); 
          PointerInput . Origin   origin   =   PointerInput . Origin . fromElement ( pointerArea ); 
 
          Sequence   actionListPen   =   new   Sequence ( pen ,   0 ) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   0 ,   0 )) 
                  . addAction ( pen . createPointerDown ( 0 )) 
                  . addAction ( pen . createPointerMove ( Duration . ZERO ,   origin ,   2 ,   2 ,   eventProperties )) 
                  . addAction ( pen . createPointerUp ( 0 )); 
 
          (( RemoteWebDriver )   driver ). perform ( Collections . singletonList ( actionListPen )); 
 
          List < WebElement >   moves   =   driver . findElements ( By . className ( "pointermove" )); 
          Map < String ,   String >   moveTo   =   getPropertyInfo ( moves . get ( 0 )); 
          Map < String ,   String >   down   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))); 
          Map < String ,   String >   moveBy   =   getPropertyInfo ( moves . get ( 1 )); 
          Map < String ,   String >   up   =   getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))); 
 
          Rectangle   rect   =   pointerArea . getRect (); 
          int   centerX   =   ( int )   Math . floor ( rect . width   /   2   +   rect . getX ()); 
          int   centerY   =   ( int )   Math . floor ( rect . height   /   2   +   rect . getY ()); 
          Assertions . assertEquals ( "-1" ,   moveTo . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveTo . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   moveTo . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   moveTo . get ( "pageY" )); 
          Assertions . assertEquals ( "0" ,   down . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   down . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX ),   down . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY ),   down . get ( "pageY" )); 
          Assertions . assertEquals ( "-1" ,   moveBy . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   moveBy . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   moveBy . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   moveBy . get ( "pageY" )); 
          Assertions . assertEquals ( "-72" ,   moveBy . get ( "tiltX" )); 
          Assertions . assertEquals ( "9" ,   moveBy . get ( "tiltY" )); 
          Assertions . assertEquals ( "86" ,   moveBy . get ( "twist" )); 
          Assertions . assertEquals ( "0" ,   up . get ( "button" )); 
          Assertions . assertEquals ( "pen" ,   up . get ( "pointerType" )); 
          Assertions . assertEquals ( String . valueOf ( centerX   +   2 ),   up . get ( "pageX" )); 
          Assertions . assertEquals ( String . valueOf ( centerY   +   2 ),   up . get ( "pageY" )); 
      } 
 
      private   Map < String ,   String >   getPropertyInfo ( WebElement   element )   { 
          String   text   =   element . getText (); 
          text   =   text . substring ( text . indexOf ( ' ' )   +   1 ); 
 
          return   Arrays . stream ( text . split ( ", " )) 
                  . map ( s   ->   s . split ( ": " )) 
                  . collect ( Collectors . toMap ( 
                          a   ->   a [ 0 ] , 
                          a   ->   a [ 1 ] 
                  )); 
      } 
 } 
     pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ,  tilt_x =- 72 ,  tilt_y = 9 ,  twist = 86 ) \
         . pointer_up ( 0 ) 
     action . perform ()  /examples/python/tests/actions_api/test_pen.py 
Copy
 
Close 
import  math 
 from  selenium.webdriver.common.actions.action_builder  import  ActionBuilder 
from  selenium.webdriver.common.actions.interaction  import  POINTER_PEN 
from  selenium.webdriver.common.actions.pointer_input  import  PointerInput 
from  selenium.webdriver.common.by  import  By 
 
 def  test_use_pen ( driver ): 
    driver . get ( 'https://www.selenium.dev/selenium/web/pointerActionsPage.html' ) 
 
     pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ) \
         . pointer_up () 
     action . perform () 
 
     moves  =  driver . find_elements ( By . CLASS_NAME ,  "pointermove" ) 
     move_to  =  properties ( moves [ 0 ]) 
     down  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerdown" )) 
     move_by  =  properties ( moves [ 1 ]) 
     up  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerup" )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect [ "x" ]  +  rect [ "width" ]  /  2 
     center_y  =  rect [ "y" ]  +  rect [ "height" ]  /  2 
 
     assert  move_to [ "button" ]  ==  "-1" 
     assert  move_to [ "pointerType" ]  ==  "pen" 
     assert  move_to [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  move_to [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  down [ "button" ]  ==  "0" 
     assert  down [ "pointerType" ]  ==  "pen" 
     assert  down [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  down [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  move_by [ "button" ]  ==  "-1" 
     assert  move_by [ "pointerType" ]  ==  "pen" 
     assert  move_by [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  move_by [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
     assert  up [ "button" ]  ==  "0" 
     assert  up [ "pointerType" ]  ==  "pen" 
     assert  up [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  up [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
 
 
 def  test_set_pointer_event_properties ( driver ): 
    driver . get ( 'https://www.selenium.dev/selenium/web/pointerActionsPage.html' ) 
 
     pointer_area  =  driver . find_element ( By . ID ,  "pointerArea" ) 
     pen_input  =  PointerInput ( POINTER_PEN ,  "default pen" ) 
     action  =  ActionBuilder ( driver ,  mouse = pen_input ) 
     action . pointer_action \
         . move_to ( pointer_area ) \
         . pointer_down () \
         . move_by ( 2 ,  2 ,  tilt_x =- 72 ,  tilt_y = 9 ,  twist = 86 ) \
         . pointer_up ( 0 ) 
     action . perform () 
 
     moves  =  driver . find_elements ( By . CLASS_NAME ,  "pointermove" ) 
     move_to  =  properties ( moves [ 0 ]) 
     down  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerdown" )) 
     move_by  =  properties ( moves [ 1 ]) 
     up  =  properties ( driver . find_element ( By . CLASS_NAME ,  "pointerup" )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect [ "x" ]  +  rect [ "width" ]  /  2 
     center_y  =  rect [ "y" ]  +  rect [ "height" ]  /  2 
 
     assert  move_to [ "button" ]  ==  "-1" 
     assert  move_to [ "pointerType" ]  ==  "pen" 
     assert  move_to [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  move_to [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  down [ "button" ]  ==  "0" 
     assert  down [ "pointerType" ]  ==  "pen" 
     assert  down [ "pageX" ]  ==  str ( math . floor ( center_x )) 
     assert  down [ "pageY" ]  ==  str ( math . floor ( center_y )) 
     assert  move_by [ "button" ]  ==  "-1" 
     assert  move_by [ "pointerType" ]  ==  "pen" 
     assert  move_by [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  move_by [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
     assert  move_by [ "tiltX" ]  ==  "-72" 
     assert  move_by [ "tiltY" ]  ==  "9" 
     assert  move_by [ "twist" ]  ==  "86" 
     assert  up [ "button" ]  ==  "0" 
     assert  up [ "pointerType" ]  ==  "pen" 
     assert  up [ "pageX" ]  ==  str ( math . floor ( center_x  +  2 )) 
     assert  up [ "pageY" ]  ==  str ( math . floor ( center_y  +  2 )) 
 
 
 def  properties ( element ): 
    kv  =  element . text . split ( ' ' ,  1 )[ 1 ] . split ( ', ' ) 
     return  { x [ 0 ]: x [ 1 ]  for  x  in  list ( map ( lambda  item :  item . split ( ': ' ),  kv ))} 
 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             PointerInputDevice . PointerEventProperties  properties  =  new  PointerInputDevice . PointerEventProperties ()  { 
                 TiltX  =  - 72 , 
                 TiltY  =  9 , 
                 Twist  =  86 , 
             };             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero ,  properties )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ());  /examples/dotnet/SeleniumDocs/ActionsAPI/PenTest.cs 
Copy
 
Close 
using  System ; 
using  System.Collections.Generic ; 
using  System.Drawing ; 
using  System.Linq ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  PenTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  UsePen () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/pointerActionsPage.html" ; 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
 
             var  moves  =  driver . FindElements ( By . ClassName ( "pointermove" )); 
             var  moveTo  =  getProperties ( moves . ElementAt ( 0 )); 
             var  down  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerdown" ))); 
             var  moveBy  =  getProperties ( moves . ElementAt ( 1 )); 
             var  up  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerup" ))); 
 
             Point  location  =  pointerArea . Location ; 
             Size  size  =  pointerArea . Size ; 
             decimal  centerX  =  location . X  +  size . Width  /  2 ; 
             decimal  centerY  =  location . Y  +  size . Height  /  2 ; 
 
             Assert . AreEqual ( "-1" ,  moveTo [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveTo [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  moveTo [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  moveTo [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  down [ "button" ]); 
             Assert . AreEqual ( "pen" ,  down [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  down [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  down [ "pageY" ])); 
             Assert . AreEqual ( "-1" ,  moveBy [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveBy [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  moveBy [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  moveBy [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  up [ "button" ]); 
             Assert . AreEqual ( "pen" ,  up [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  up [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  up [ "pageY" ])); 
         } 
 
         [TestMethod] 
        public  void  SetPointerEventProperties () 
         { 
             driver . Url  =  "https://selenium.dev/selenium/web/pointerActionsPage.html" ; 
 
             IWebElement  pointerArea  =  driver . FindElement ( By . Id ( "pointerArea" )); 
             ActionBuilder  actionBuilder  =  new  ActionBuilder (); 
             PointerInputDevice  pen  =  new  PointerInputDevice ( PointerKind . Pen ,  "default pen" ); 
             PointerInputDevice . PointerEventProperties  properties  =  new  PointerInputDevice . PointerEventProperties ()  { 
                 TiltX  =  - 72 , 
                 TiltY  =  9 , 
                 Twist  =  86 , 
             };             
             actionBuilder . AddAction ( pen . CreatePointerMove ( pointerArea ,  0 ,  0 ,  TimeSpan . FromMilliseconds ( 800 ))); 
             actionBuilder . AddAction ( pen . CreatePointerDown ( MouseButton . Left )); 
             actionBuilder . AddAction ( pen . CreatePointerMove ( CoordinateOrigin . Pointer , 
                 2 ,  2 ,  TimeSpan . Zero ,  properties )); 
             actionBuilder . AddAction ( pen . CreatePointerUp ( MouseButton . Left )); 
             (( IActionExecutor ) driver ). PerformActions ( actionBuilder . ToActionSequenceList ()); 
             
             var  moves  =  driver . FindElements ( By . ClassName ( "pointermove" )); 
             var  moveTo  =  getProperties ( moves . ElementAt ( 0 )); 
             var  down  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerdown" ))); 
             var  moveBy  =  getProperties ( moves . ElementAt ( 1 )); 
             var  up  =  getProperties ( driver . FindElement ( By . ClassName ( "pointerup" ))); 
 
             Point  location  =  pointerArea . Location ; 
             Size  size  =  pointerArea . Size ; 
             decimal  centerX  =  location . X  +  size . Width  /  2 ; 
             decimal  centerY  =  location . Y  +  size . Height  /  2 ; 
 
             Assert . AreEqual ( "-1" ,  moveTo [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveTo [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  moveTo [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  moveTo [ "pageY" ])); 
             Assert . AreEqual ( "0" ,  down [ "button" ]); 
             Assert . AreEqual ( "pen" ,  down [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX ,  down [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY ,  down [ "pageY" ])); 
             Assert . AreEqual ( "-1" ,  moveBy [ "button" ]); 
             Assert . AreEqual ( "pen" ,  moveBy [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  moveBy [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  moveBy [ "pageY" ])); 
             Assert . AreEqual ((- 72 ). ToString (),  moveBy [ "tiltX" ]); 
             Assert . AreEqual (( 9 ). ToString (),  moveBy [ "tiltY" ]); 
             Assert . AreEqual (( 86 ). ToString (),  moveBy [ "twist" ]); 
             Assert . AreEqual ( "0" ,  up [ "button" ]); 
             Assert . AreEqual ( "pen" ,  up [ "pointerType" ]); 
             Assert . IsTrue ( VerifyEquivalent ( centerX  +  2 ,  up [ "pageX" ])); 
             Assert . IsTrue ( VerifyEquivalent ( centerY  +  2 ,  up [ "pageY" ])); 
         } 
 
         private  Dictionary < string ,  string >  getProperties ( IWebElement  element ) 
         { 
             var  str  =  element . Text ; 
             str  =  str [( str . Split ()[ 0 ]. Length  +  1 )..]; 
             IEnumerable < string []>  keyValue  =  str . Split ( ", " ). Select ( part  =>  part . Split ( ":" )); 
             return  keyValue . ToDictionary ( split  =>  split [ 0 ]. Trim (),  split  =>  split [ 1 ]. Trim ()); 
         } 
 
         private  bool  VerifyEquivalent ( decimal  expected ,  string  actual ) 
         { 
             var  absolute  =  Math . Abs ( expected  -  decimal . Parse ( actual )); 
             if  ( absolute  <=  1 ) 
             { 
                 return  true ; 
             } 
 
             throw  new  Exception ( "Expected: "  +  expected  +  "; but received: "  +  actual ); 
         } 
     } 
 }     pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ,  tilt_x :  - 72 ,  tilt_y :  9 ,  twist :  86 ) 
           . pointer_up 
           . perform  /examples/ruby/spec/actions_api/pen_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Pen'  do 
  let ( :driver )  {  start_session  } 
 
   it  'uses a pen'  do 
     driver . get  'https://www.selenium.dev/selenium/web/pointerActionsPage.html' 
 
     pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ) 
           . pointer_up 
           . perform 
 
     moves  =  driver . find_elements ( class :  'pointermove' ) 
     move_to  =  properties ( moves [ 0 ] ) 
     down  =  properties ( driver . find_element ( class :  'pointerdown' )) 
     move_by  =  properties ( moves [ 1 ] ) 
     up  =  properties ( driver . find_element ( class :  'pointerup' )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect . x  +  ( rect . width  /  2 ) 
     center_y  =  rect . y  +  ( rect . height  /  2 ) 
 
     expect ( move_to ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  center_x . to_s , 
                                'pageY'  =>  center_y . floor . to_s ) 
     expect ( down ) . to  include ( 'button'  =>  '0' , 
                             'pointerType'  =>  'pen' , 
                             'pageX'  =>  center_x . to_s , 
                             'pageY'  =>  center_y . floor . to_s ) 
     expect ( move_by ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  ( center_x  +  2 ) . to_s , 
                                'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
     expect ( up ) . to  include ( 'button'  =>  '0' , 
                           'pointerType'  =>  'pen' , 
                           'pageX'  =>  ( center_x  +  2 ) . to_s , 
                           'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
   end 
 
   it  'sets pointer event attributes'  do 
     driver . get  'https://www.selenium.dev/selenium/web/pointerActionsPage.html' 
 
     pointer_area  =  driver . find_element ( id :  'pointerArea' ) 
     driver . action ( devices :  :pen ) 
           . move_to ( pointer_area ) 
           . pointer_down 
           . move_by ( 2 ,  2 ,  tilt_x :  - 72 ,  tilt_y :  9 ,  twist :  86 ) 
           . pointer_up 
           . perform 
 
     moves  =  driver . find_elements ( class :  'pointermove' ) 
     move_to  =  properties ( moves [ 0 ] ) 
     down  =  properties ( driver . find_element ( class :  'pointerdown' )) 
     move_by  =  properties ( moves [ 1 ] ) 
     up  =  properties ( driver . find_element ( class :  'pointerup' )) 
 
     rect  =  pointer_area . rect 
     center_x  =  rect . x  +  ( rect . width  /  2 ) 
     center_y  =  rect . y  +  ( rect . height  /  2 ) 
 
     expect ( move_to ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  center_x . to_s , 
                                'pageY'  =>  center_y . floor . to_s ) 
     expect ( down ) . to  include ( 'button'  =>  '0' , 
                             'pointerType'  =>  'pen' , 
                             'pageX'  =>  center_x . to_s , 
                             'pageY'  =>  center_y . floor . to_s ) 
     expect ( move_by ) . to  include ( 'button'  =>  '-1' , 
                                'pointerType'  =>  'pen' , 
                                'pageX'  =>  ( center_x  +  2 ) . to_s , 
                                'pageY'  =>  ( center_y  +  2 ) . floor . to_s , 
                                'tiltX'  =>  - 72 . to_s , 
                                'tiltY'  =>  9 . to_s , 
                                'twist'  =>  86 . to_s ) 
     expect ( up ) . to  include ( 'button'  =>  '0' , 
                           'pointerType'  =>  'pen' , 
                           'pageX'  =>  ( center_x  +  2 ) . to_s , 
                           'pageY'  =>  ( center_y  +  2 ) . floor . to_s ) 
   end 
 
   def  properties ( element ) 
     element . text . sub ( /.*?\s/ ,  '' ) . split ( ',' ) . to_h  {  | item |  item . lstrip . split ( /\s*:\s*/ )  } 
   end 
 end 
        val  pen  =  PointerInput ( PointerInput . Kind . PEN ,  "default pen" ) 
         val  eventProperties  =  PointerInput . eventProperties () 
                 . setTiltX (- 72 ) 
                 . setTiltY ( 9 ) 
                 . setTwist ( 86 ) 
         val  origin  =  PointerInput . Origin . fromElement ( pointerArea ) 
         
         val  actionListPen  =  Sequence ( pen ,  0 ) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  0 ,  0 )) 
                 . addAction ( pen . createPointerDown ( 0 )) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  2 ,  2 ,  eventProperties )) 
                 . addAction ( pen . createPointerUp ( 0 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( listOf ( actionListPen )) 
                     
 /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/PenTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.PointerInput 
import  org.openqa.selenium.interactions.Sequence 
import  org.openqa.selenium.remote.RemoteWebDriver 
 import  kotlin.collections.Map 
import  java.time.Duration 
 class  PenTest  :  BaseTest ()  { 
  
     @Test 
     fun  usePen ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/pointerActionsPage.html" ) 
         
         val  pointerArea  =  driver . findElement ( By . id ( "pointerArea" )) 
         Actions ( driver ) 
                 . setActivePointer ( PointerInput . Kind . PEN ,  "default pen" ) 
                 . moveToElement ( pointerArea ) 
                 . clickAndHold () 
                 . moveByOffset ( 2 ,  2 ) 
                 . release () 
                 . perform () 
 
         val  moves  =  driver . findElements ( By . className ( "pointermove" )) 
         val  moveTo  =  getPropertyInfo ( moves . get ( 0 )) 
         val  down  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))) 
         val  moveBy  =  getPropertyInfo ( moves . get ( 1 )) 
         val  up  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))) 
         
         val  rect  =  pointerArea . getRect () 
 
         val  centerX  =  Math . floor ( rect . width . toDouble ()  /  2  +  rect . getX ()). toInt () 
         val  centerY  =  Math . floor ( rect . height . toDouble ()  /  2  +  rect . getY ()). toInt () 
         Assertions . assertEquals ( "-1" ,  moveTo . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveTo . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  moveTo . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  moveTo . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  down . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  down . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  down . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  down . get ( "pageY" )) 
         Assertions . assertEquals ( "-1" ,  moveBy . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveBy . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  moveBy . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  moveBy . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  up . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  up . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  up . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  up . get ( "pageY" )) 
     } 
 
     @Test 
     fun  setPointerEventProperties ()  { 
         driver . get ( "https://selenium.dev/selenium/web/pointerActionsPage.html" ) 
         
         val  pointerArea  =  driver . findElement ( By . id ( "pointerArea" )) 
         val  pen  =  PointerInput ( PointerInput . Kind . PEN ,  "default pen" ) 
         val  eventProperties  =  PointerInput . eventProperties () 
                 . setTiltX (- 72 ) 
                 . setTiltY ( 9 ) 
                 . setTwist ( 86 ) 
         val  origin  =  PointerInput . Origin . fromElement ( pointerArea ) 
         
         val  actionListPen  =  Sequence ( pen ,  0 ) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  0 ,  0 )) 
                 . addAction ( pen . createPointerDown ( 0 )) 
                 . addAction ( pen . createPointerMove ( Duration . ZERO ,  origin ,  2 ,  2 ,  eventProperties )) 
                 . addAction ( pen . createPointerUp ( 0 )) 
 
         ( driver  as  RemoteWebDriver ). perform ( listOf ( actionListPen )) 
                     
 
         val  moves  =  driver . findElements ( By . className ( "pointermove" )) 
         val  moveTo  =  getPropertyInfo ( moves . get ( 0 )) 
         val  down  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerdown" ))) 
         val  moveBy  =  getPropertyInfo ( moves . get ( 1 )) 
         val  up  =  getPropertyInfo ( driver . findElement ( By . className ( "pointerup" ))) 
         
         val  rect  =  pointerArea . getRect () 
 
         val  centerX  =  Math . floor ( rect . width . toDouble ()  /  2  +  rect . getX ()). toInt () 
         val  centerY  =  Math . floor ( rect . height . toDouble ()  /  2  +  rect . getY ()). toInt () 
         Assertions . assertEquals ( "-1" ,  moveTo . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveTo . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  moveTo . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  moveTo . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  down . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  down . get ( "pointerType" )) 
         Assertions . assertEquals ( centerX . toString (),  down . get ( "pageX" )) 
         Assertions . assertEquals ( centerY . toString (),  down . get ( "pageY" )) 
         Assertions . assertEquals ( "-1" ,  moveBy . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  moveBy . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  moveBy . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  moveBy . get ( "pageY" )) 
         Assertions . assertEquals ( "0" ,  up . get ( "button" )) 
         Assertions . assertEquals ( "pen" ,  up . get ( "pointerType" )) 
         Assertions . assertEquals (( centerX  +  2 ). toString (),  up . get ( "pageX" )) 
         Assertions . assertEquals (( centerY  +  2 ). toString (),  up . get ( "pageY" ))  
 	    
         Assertions . assertEquals ( "-72" ,  moveBy . get ( "tiltX" )) 
         Assertions . assertEquals ( "9" ,  moveBy . get ( "tiltY" )) 
         Assertions . assertEquals ( "86" ,  moveBy . get ( "twist" )) 
     } 
     
     fun  getPropertyInfo ( element :  WebElement ):  Map < String ,  String >  { 
         var  text  =  element . getText ()   
         text  =  text . substring ( text . indexOf ( " " )+ 1 ) 
         return  text . split ( ", " ) 
         . map  {  it . split ( ": " )  } 
         . map  {  it . first ()  to  it . last ()  } 
         . toMap ()  
     } 
 } 4 - Scroll wheel actions A representation of a scroll wheel input device for interacting with a web page.
Selenium v4.2 
Chromium Only 
There are 5 scenarios for scrolling on a page.
This is the most common scenario. Unlike traditional click and send keys methods,
the actions class does not automatically scroll the target element into view,
so this method will need to be used if elements are not already inside the viewport.
This method takes a web element as the sole argument.
Regardless of whether the element is above or below the current viewscreen,
the viewport will be scrolled so the bottom of the element is at the bottom of the screen.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.JavascriptExecutor ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.WheelInput ; 
 
 public   class  WheelTest   extends   BaseChromeTest   { 
      @Test 
      public   void   shouldScrollToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( iframe )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( footer )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmountWithOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmountFromOrigin ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ); 
 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      private   boolean   inViewport ( WebElement   element )   { 
 
          String   script   = 
                  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                          +   "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                          +   "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                          +   "window.pageYOffset&&t+o>window.pageXOffset" ; 
 
          return   ( boolean )   (( JavascriptExecutor )   driver ). executeScript ( script ,   element ); 
      } 
 } 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform ()  /examples/python/tests/actions_api/test_wheel.py 
Copy
 
Close 
from  time  import  sleep 
 from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.common.actions.wheel_input  import  ScrollOrigin 
 
 def  test_can_scroll_to_element ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform () 
 
     assert  _in_viewport ( driver ,  iframe ) 
 
 
 def  test_can_scroll_from_viewport_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  _in_viewport ( driver ,  footer ) 
 
 
 def  test_can_scroll_from_element_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_element_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_viewport_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  _in_viewport ( driver ,  element ): 
    script  =  ( 
         "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n " 
         "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n " 
         "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n " 
         "window.pageYOffset&&t+o>window.pageXOffset" 
     ) 
     return  driver . execute_script ( script ,  element ) 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  WheelTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ShouldAllowScrollingToAnElement () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( iframe )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( footer )); 
         } 
 
         [TestMethod] 
        public  void  ShouldScrollFromElementByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromElementByGivenAmountWithOffset () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmountFromOrigin () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ; 
 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         private  bool  IsInViewport ( IWebElement  element ) 
         { 
             String  script  = 
                 "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                 +  "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                 +  "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                 +  "window.pageYOffset&&t+o>window.pageXOffset" ; 
             IJavaScriptExecutor  javascriptDriver  =  this . driver  as  IJavaScriptExecutor ; 
 
             return  ( bool ) javascriptDriver . ExecuteScript ( script ,  element ); 
         } 
     } 
 }     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform  /examples/ruby/spec/actions_api/wheel_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Scrolling'  do 
  let ( :driver )  {  start_session  } 
 
   it  'scrolls to element'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform 
 
     expect ( in_viewport? ( iframe )) . to  eq  true 
   end 
 
   it  'scrolls by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform 
 
     expect ( in_viewport? ( footer )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer ,  0 ,  - 50 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' ) 
 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 end 
 def  in_viewport? ( element ) 
  in_viewport  =  <<~ IN_VIEWPORT 
     for ( var  e = arguments [ 0 ] , f = e . offsetTop , t = e . offsetLeft , o = e . offsetWidth , n = e . offsetHeight ; 
     e . offsetParent ;) f += ( e = e . offsetParent ) . offsetTop , t += e . offsetLeft ; 
     return  f < window . pageYOffset + window . innerHeight && t < window . pageXOffset + window . innerWidth && f + n > 
     window . pageYOffset && t + o > window . pageXOffset 
   IN_VIEWPORT 
 
   driver . execute_script ( in_viewport ,  element ) 
 end 
    const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
 /examples/javascript/test/actionsApi/wheelTest.spec.js 
Copy
 
Close 
const  {  By ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Wheel Tests' ,  function  ()  { 
  let  driver 
 
   before ( async  function  ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async ()  =>  await  driver . quit ()) 
 
   it ( 'Scroll to element' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
     assert . ok ( await  inViewport ( iframe )) 
   }) 
 
   it ( 'Scroll by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
     assert . ok ( await  inViewport ( footer )) 
   }) 
 
   it ( 'Scroll from an element by a given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an element with an offset' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  - 50 ,  0 ,  200 ,  footer ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an offset of origin (element) by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   function  inViewport ( element )  { 
     return  driver . executeScript ( "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" ,  element ) 
   } 
 })         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform ()  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.JavascriptExecutor 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.WheelInput 
 class  WheelTest  :  BaseTest ()  { 
    
     @Test 
     fun  shouldScrollToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( iframe )) 
     } 
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( footer )) 
     }    
     
     @Test 
     fun  shouldScrollFromElementByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     @Test 
     fun  shouldScrollFromElementByGivenAmountWithOffset ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     }     
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmountFromOrigin ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     fun  inViewport ( element :  WebElement ):  Boolean  { 
         val  script  =  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n window.pageYOffset&&t+o>window.pageXOffset" 
         return  ( driver  as  JavascriptExecutor ). executeScript ( script ,  element )  as  Boolean  
     } 
 } 
This is the second most common scenario for scrolling. Pass in an delta x and a delta y value for how much to scroll
in the right and down directions. Negative values represent left and up, respectively.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.JavascriptExecutor ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.WheelInput ; 
 
 public   class  WheelTest   extends   BaseChromeTest   { 
      @Test 
      public   void   shouldScrollToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( iframe )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( footer )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmountWithOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmountFromOrigin ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ); 
 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      private   boolean   inViewport ( WebElement   element )   { 
 
          String   script   = 
                  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                          +   "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                          +   "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                          +   "window.pageYOffset&&t+o>window.pageXOffset" ; 
 
          return   ( boolean )   (( JavascriptExecutor )   driver ). executeScript ( script ,   element ); 
      } 
 } 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform ()  /examples/python/tests/actions_api/test_wheel.py 
Copy
 
Close 
from  time  import  sleep 
 from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.common.actions.wheel_input  import  ScrollOrigin 
 
 def  test_can_scroll_to_element ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform () 
 
     assert  _in_viewport ( driver ,  iframe ) 
 
 
 def  test_can_scroll_from_viewport_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  _in_viewport ( driver ,  footer ) 
 
 
 def  test_can_scroll_from_element_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_element_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_viewport_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  _in_viewport ( driver ,  element ): 
    script  =  ( 
         "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n " 
         "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n " 
         "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n " 
         "window.pageYOffset&&t+o>window.pageXOffset" 
     ) 
     return  driver . execute_script ( script ,  element ) 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  WheelTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ShouldAllowScrollingToAnElement () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( iframe )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( footer )); 
         } 
 
         [TestMethod] 
        public  void  ShouldScrollFromElementByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromElementByGivenAmountWithOffset () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmountFromOrigin () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ; 
 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         private  bool  IsInViewport ( IWebElement  element ) 
         { 
             String  script  = 
                 "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                 +  "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                 +  "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                 +  "window.pageYOffset&&t+o>window.pageXOffset" ; 
             IJavaScriptExecutor  javascriptDriver  =  this . driver  as  IJavaScriptExecutor ; 
 
             return  ( bool ) javascriptDriver . ExecuteScript ( script ,  element ); 
         } 
     } 
 }     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform  /examples/ruby/spec/actions_api/wheel_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Scrolling'  do 
  let ( :driver )  {  start_session  } 
 
   it  'scrolls to element'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform 
 
     expect ( in_viewport? ( iframe )) . to  eq  true 
   end 
 
   it  'scrolls by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform 
 
     expect ( in_viewport? ( footer )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer ,  0 ,  - 50 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' ) 
 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 end 
 def  in_viewport? ( element ) 
  in_viewport  =  <<~ IN_VIEWPORT 
     for ( var  e = arguments [ 0 ] , f = e . offsetTop , t = e . offsetLeft , o = e . offsetWidth , n = e . offsetHeight ; 
     e . offsetParent ;) f += ( e = e . offsetParent ) . offsetTop , t += e . offsetLeft ; 
     return  f < window . pageYOffset + window . innerHeight && t < window . pageXOffset + window . innerWidth && f + n > 
     window . pageYOffset && t + o > window . pageXOffset 
   IN_VIEWPORT 
 
   driver . execute_script ( in_viewport ,  element ) 
 end 
    const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 /examples/javascript/test/actionsApi/wheelTest.spec.js 
Copy
 
Close 
const  {  By ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Wheel Tests' ,  function  ()  { 
  let  driver 
 
   before ( async  function  ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async ()  =>  await  driver . quit ()) 
 
   it ( 'Scroll to element' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
     assert . ok ( await  inViewport ( iframe )) 
   }) 
 
   it ( 'Scroll by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
     assert . ok ( await  inViewport ( footer )) 
   }) 
 
   it ( 'Scroll from an element by a given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an element with an offset' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  - 50 ,  0 ,  200 ,  footer ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an offset of origin (element) by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   function  inViewport ( element )  { 
     return  driver . executeScript ( "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" ,  element ) 
   } 
 })         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform ()  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.JavascriptExecutor 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.WheelInput 
 class  WheelTest  :  BaseTest ()  { 
    
     @Test 
     fun  shouldScrollToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( iframe )) 
     } 
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( footer )) 
     }    
     
     @Test 
     fun  shouldScrollFromElementByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     @Test 
     fun  shouldScrollFromElementByGivenAmountWithOffset ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     }     
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmountFromOrigin ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     fun  inViewport ( element :  WebElement ):  Boolean  { 
         val  script  =  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n window.pageYOffset&&t+o>window.pageXOffset" 
         return  ( driver  as  JavascriptExecutor ). executeScript ( script ,  element )  as  Boolean  
     } 
 } 
This scenario is effectively a combination of the above two methods.
To execute this use the “Scroll From” method, which takes 3 arguments.
The first represents the origination point, which we designate as the element,
and the second two are the delta x and delta y values.
If the element is out of the viewport,
it will be scrolled to the bottom of the screen, then the page will be scrolled by the provided
delta x and delta y values.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.JavascriptExecutor ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.WheelInput ; 
 
 public   class  WheelTest   extends   BaseChromeTest   { 
      @Test 
      public   void   shouldScrollToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( iframe )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( footer )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmountWithOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmountFromOrigin ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ); 
 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      private   boolean   inViewport ( WebElement   element )   { 
 
          String   script   = 
                  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                          +   "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                          +   "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                          +   "window.pageYOffset&&t+o>window.pageXOffset" ; 
 
          return   ( boolean )   (( JavascriptExecutor )   driver ). executeScript ( script ,   element ); 
      } 
 } 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform ()  /examples/python/tests/actions_api/test_wheel.py 
Copy
 
Close 
from  time  import  sleep 
 from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.common.actions.wheel_input  import  ScrollOrigin 
 
 def  test_can_scroll_to_element ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform () 
 
     assert  _in_viewport ( driver ,  iframe ) 
 
 
 def  test_can_scroll_from_viewport_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  _in_viewport ( driver ,  footer ) 
 
 
 def  test_can_scroll_from_element_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_element_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_viewport_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  _in_viewport ( driver ,  element ): 
    script  =  ( 
         "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n " 
         "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n " 
         "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n " 
         "window.pageYOffset&&t+o>window.pageXOffset" 
     ) 
     return  driver . execute_script ( script ,  element ) 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  WheelTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ShouldAllowScrollingToAnElement () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( iframe )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( footer )); 
         } 
 
         [TestMethod] 
        public  void  ShouldScrollFromElementByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromElementByGivenAmountWithOffset () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmountFromOrigin () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ; 
 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         private  bool  IsInViewport ( IWebElement  element ) 
         { 
             String  script  = 
                 "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                 +  "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                 +  "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                 +  "window.pageYOffset&&t+o>window.pageXOffset" ; 
             IJavaScriptExecutor  javascriptDriver  =  this . driver  as  IJavaScriptExecutor ; 
 
             return  ( bool ) javascriptDriver . ExecuteScript ( script ,  element ); 
         } 
     } 
 }     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform  /examples/ruby/spec/actions_api/wheel_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Scrolling'  do 
  let ( :driver )  {  start_session  } 
 
   it  'scrolls to element'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform 
 
     expect ( in_viewport? ( iframe )) . to  eq  true 
   end 
 
   it  'scrolls by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform 
 
     expect ( in_viewport? ( footer )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer ,  0 ,  - 50 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' ) 
 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 end 
 def  in_viewport? ( element ) 
  in_viewport  =  <<~ IN_VIEWPORT 
     for ( var  e = arguments [ 0 ] , f = e . offsetTop , t = e . offsetLeft , o = e . offsetWidth , n = e . offsetHeight ; 
     e . offsetParent ;) f += ( e = e . offsetParent ) . offsetTop , t += e . offsetLeft ; 
     return  f < window . pageYOffset + window . innerHeight && t < window . pageXOffset + window . innerWidth && f + n > 
     window . pageYOffset && t + o > window . pageXOffset 
   IN_VIEWPORT 
 
   driver . execute_script ( in_viewport ,  element ) 
 end 
    const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 /examples/javascript/test/actionsApi/wheelTest.spec.js 
Copy
 
Close 
const  {  By ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Wheel Tests' ,  function  ()  { 
  let  driver 
 
   before ( async  function  ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async ()  =>  await  driver . quit ()) 
 
   it ( 'Scroll to element' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
     assert . ok ( await  inViewport ( iframe )) 
   }) 
 
   it ( 'Scroll by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
     assert . ok ( await  inViewport ( footer )) 
   }) 
 
   it ( 'Scroll from an element by a given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an element with an offset' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  - 50 ,  0 ,  200 ,  footer ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an offset of origin (element) by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   function  inViewport ( element )  { 
     return  driver . executeScript ( "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" ,  element ) 
   } 
 })         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform ()  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.JavascriptExecutor 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.WheelInput 
 class  WheelTest  :  BaseTest ()  { 
    
     @Test 
     fun  shouldScrollToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( iframe )) 
     } 
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( footer )) 
     }    
     
     @Test 
     fun  shouldScrollFromElementByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     @Test 
     fun  shouldScrollFromElementByGivenAmountWithOffset ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     }     
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmountFromOrigin ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     fun  inViewport ( element :  WebElement ):  Boolean  { 
         val  script  =  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n window.pageYOffset&&t+o>window.pageXOffset" 
         return  ( driver  as  JavascriptExecutor ). executeScript ( script ,  element )  as  Boolean  
     } 
 } 
This scenario is used when you need to scroll only a portion of the screen, and it is outside the viewport.
Or is inside the viewport and the portion of the screen that must be scrolled
is a known offset away from a specific element.
This uses the “Scroll From” method again, and in addition to specifying the element,
an offset is specified to indicate the origin point of the scroll. The offset is
calculated from the center of the provided element.
If the element is out of the viewport,
it first will be scrolled to the bottom of the screen, then the origin of the scroll will be determined
by adding the offset to the coordinates of the center of the element, and finally
the page will be scrolled by the provided delta x and delta y values.
Note that if the offset from the center of the element falls outside of the viewport,
it will result in an exception.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.JavascriptExecutor ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.WheelInput ; 
 
 public   class  WheelTest   extends   BaseChromeTest   { 
      @Test 
      public   void   shouldScrollToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( iframe )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( footer )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmountWithOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmountFromOrigin ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ); 
 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      private   boolean   inViewport ( WebElement   element )   { 
 
          String   script   = 
                  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                          +   "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                          +   "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                          +   "window.pageYOffset&&t+o>window.pageXOffset" ; 
 
          return   ( boolean )   (( JavascriptExecutor )   driver ). executeScript ( script ,   element ); 
      } 
 } 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform ()  /examples/python/tests/actions_api/test_wheel.py 
Copy
 
Close 
from  time  import  sleep 
 from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.common.actions.wheel_input  import  ScrollOrigin 
 
 def  test_can_scroll_to_element ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform () 
 
     assert  _in_viewport ( driver ,  iframe ) 
 
 
 def  test_can_scroll_from_viewport_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  _in_viewport ( driver ,  footer ) 
 
 
 def  test_can_scroll_from_element_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_element_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_viewport_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  _in_viewport ( driver ,  element ): 
    script  =  ( 
         "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n " 
         "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n " 
         "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n " 
         "window.pageYOffset&&t+o>window.pageXOffset" 
     ) 
     return  driver . execute_script ( script ,  element ) 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  WheelTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ShouldAllowScrollingToAnElement () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( iframe )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( footer )); 
         } 
 
         [TestMethod] 
        public  void  ShouldScrollFromElementByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromElementByGivenAmountWithOffset () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmountFromOrigin () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ; 
 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         private  bool  IsInViewport ( IWebElement  element ) 
         { 
             String  script  = 
                 "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                 +  "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                 +  "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                 +  "window.pageYOffset&&t+o>window.pageXOffset" ; 
             IJavaScriptExecutor  javascriptDriver  =  this . driver  as  IJavaScriptExecutor ; 
 
             return  ( bool ) javascriptDriver . ExecuteScript ( script ,  element ); 
         } 
     } 
 }     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' )  /examples/ruby/spec/actions_api/wheel_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Scrolling'  do 
  let ( :driver )  {  start_session  } 
 
   it  'scrolls to element'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform 
 
     expect ( in_viewport? ( iframe )) . to  eq  true 
   end 
 
   it  'scrolls by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform 
 
     expect ( in_viewport? ( footer )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer ,  0 ,  - 50 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' ) 
 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 end 
 def  in_viewport? ( element ) 
  in_viewport  =  <<~ IN_VIEWPORT 
     for ( var  e = arguments [ 0 ] , f = e . offsetTop , t = e . offsetLeft , o = e . offsetWidth , n = e . offsetHeight ; 
     e . offsetParent ;) f += ( e = e . offsetParent ) . offsetTop , t += e . offsetLeft ; 
     return  f < window . pageYOffset + window . innerHeight && t < window . pageXOffset + window . innerWidth && f + n > 
     window . pageYOffset && t + o > window . pageXOffset 
   IN_VIEWPORT 
 
   driver . execute_script ( in_viewport ,  element ) 
 end 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
 /examples/javascript/test/actionsApi/wheelTest.spec.js 
Copy
 
Close 
const  {  By ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Wheel Tests' ,  function  ()  { 
  let  driver 
 
   before ( async  function  ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async ()  =>  await  driver . quit ()) 
 
   it ( 'Scroll to element' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
     assert . ok ( await  inViewport ( iframe )) 
   }) 
 
   it ( 'Scroll by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
     assert . ok ( await  inViewport ( footer )) 
   }) 
 
   it ( 'Scroll from an element by a given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an element with an offset' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  - 50 ,  0 ,  200 ,  footer ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an offset of origin (element) by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   function  inViewport ( element )  { 
     return  driver . executeScript ( "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" ,  element ) 
   } 
 })         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform ()  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.JavascriptExecutor 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.WheelInput 
 class  WheelTest  :  BaseTest ()  { 
    
     @Test 
     fun  shouldScrollToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( iframe )) 
     } 
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( footer )) 
     }    
     
     @Test 
     fun  shouldScrollFromElementByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     @Test 
     fun  shouldScrollFromElementByGivenAmountWithOffset ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     }     
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmountFromOrigin ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     fun  inViewport ( element :  WebElement ):  Boolean  { 
         val  script  =  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n window.pageYOffset&&t+o>window.pageXOffset" 
         return  ( driver  as  JavascriptExecutor ). executeScript ( script ,  element )  as  Boolean  
     } 
 } 
The final scenario is used when you need to scroll only a portion of the screen,
and it is already inside the viewport.
This uses the “Scroll From” method again, but the viewport is designated instead
of an element. An offset is specified from the upper left corner of the
current viewport. After the origin point is determined,
the page will be scrolled by the provided delta x and delta y values.
Note that if the offset from the upper left corner of the viewport falls outside of the screen,
it will result in an exception.
Java 
Python 
CSharp 
Ruby 
JavaScript 
Kotlin          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); /examples/java/src/test/java/dev/selenium/actions_api/WheelTest.java 
Copy
 
Close 
package   dev.selenium.actions_api ; 
 
 import   dev.selenium.BaseChromeTest ; 
 import   org.junit.jupiter.api.Assertions ; 
 import   org.junit.jupiter.api.Test ; 
 import   org.openqa.selenium.By ; 
 import   org.openqa.selenium.JavascriptExecutor ; 
 import   org.openqa.selenium.WebElement ; 
 import   org.openqa.selenium.interactions.Actions ; 
 import   org.openqa.selenium.interactions.WheelInput ; 
 
 public   class  WheelTest   extends   BaseChromeTest   { 
      @Test 
      public   void   shouldScrollToElement ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          new   Actions ( driver ) 
                  . scrollToElement ( iframe ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( iframe )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          int   deltaY   =   footer . getRect (). y ; 
          new   Actions ( driver ) 
                  . scrollByAmount ( 0 ,   deltaY ) 
                  . perform (); 
 
          Assertions . assertTrue ( inViewport ( footer )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmount ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( iframe ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromElementByGivenAmountWithOffset ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ); 
 
          WebElement   footer   =   driver . findElement ( By . tagName ( "footer" )); 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromElement ( footer ,   0 ,   - 50 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin , 0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      @Test 
      public   void   shouldScrollFromViewportByGivenAmountFromOrigin ()   { 
          driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ); 
 
          WheelInput . ScrollOrigin   scrollOrigin   =   WheelInput . ScrollOrigin . fromViewport ( 10 ,   10 ); 
          new   Actions ( driver ) 
                  . scrollFromOrigin ( scrollOrigin ,   0 ,   200 ) 
                  . perform (); 
 
          WebElement   iframe   =   driver . findElement ( By . tagName ( "iframe" )); 
          driver . switchTo (). frame ( iframe ); 
          WebElement   checkbox   =   driver . findElement ( By . name ( "scroll_checkbox" )); 
          Assertions . assertTrue ( inViewport ( checkbox )); 
      } 
 
      private   boolean   inViewport ( WebElement   element )   { 
 
          String   script   = 
                  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                          +   "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                          +   "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                          +   "window.pageYOffset&&t+o>window.pageXOffset" ; 
 
          return   ( boolean )   (( JavascriptExecutor )   driver ). executeScript ( script ,   element ); 
      } 
 } 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform ()  /examples/python/tests/actions_api/test_wheel.py 
Copy
 
Close 
from  time  import  sleep 
 from  selenium.webdriver  import  ActionChains 
from  selenium.webdriver.common.by  import  By 
from  selenium.webdriver.common.actions.wheel_input  import  ScrollOrigin 
 
 def  test_can_scroll_to_element ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     ActionChains ( driver ) \
         . scroll_to_element ( iframe ) \
         . perform () 
 
     assert  _in_viewport ( driver ,  iframe ) 
 
 
 def  test_can_scroll_from_viewport_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     delta_y  =  footer . rect [ 'y' ] 
     ActionChains ( driver ) \
         . scroll_by_amount ( 0 ,  delta_y ) \
         . perform () 
 
     sleep ( 0.5 ) 
     assert  _in_viewport ( driver ,  footer ) 
 
 
 def  test_can_scroll_from_element_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     scroll_origin  =  ScrollOrigin . from_element ( iframe ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_element_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     footer  =  driver . find_element ( By . TAG_NAME ,  "footer" ) 
     scroll_origin  =  ScrollOrigin . from_element ( footer ,  0 ,  - 50 ) 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  test_can_scroll_from_viewport_with_offset_by_amount ( driver ): 
    driver . get ( "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     scroll_origin  =  ScrollOrigin . from_viewport ( 10 ,  10 ) 
 
     ActionChains ( driver ) \
         . scroll_from_origin ( scroll_origin ,  0 ,  200 ) \
         . perform () 
 
     sleep ( 0.5 ) 
     iframe  =  driver . find_element ( By . TAG_NAME ,  "iframe" ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( By . NAME ,  "scroll_checkbox" ) 
     assert  _in_viewport ( driver ,  checkbox ) 
 
 
 def  _in_viewport ( driver ,  element ): 
    script  =  ( 
         "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n " 
         "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n " 
         "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n " 
         "window.pageYOffset&&t+o>window.pageXOffset" 
     ) 
     return  driver . execute_script ( script ,  element ) 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform ();  /examples/dotnet/SeleniumDocs/ActionsAPI/WheelTest.cs 
Copy
 
Close 
using  System ; 
using  Microsoft.VisualStudio.TestTools.UnitTesting ; 
using  OpenQA.Selenium ; 
using  OpenQA.Selenium.Interactions ; 
 namespace  SeleniumDocs.ActionsAPI 
{ 
    [TestClass] 
    public  class  WheelTest  :  BaseChromeTest 
     { 
         [TestMethod] 
        public  void  ShouldAllowScrollingToAnElement () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             new  Actions ( driver ) 
                 . ScrollToElement ( iframe ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( iframe )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             int  deltaY  =  footer . Location . Y ; 
             new  Actions ( driver ) 
                 . ScrollByAmount ( 0 ,  deltaY ) 
                 . Perform (); 
 
             Assert . IsTrue ( IsInViewport ( footer )); 
         } 
 
         [TestMethod] 
        public  void  ShouldScrollFromElementByGivenAmount () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             WheelInputDevice . ScrollOrigin  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  iframe 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromElementByGivenAmountWithOffset () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ; 
 
             IWebElement  footer  =  driver . FindElement ( By . TagName ( "footer" )); 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Element  =  footer , 
                 XOffset  =  0 , 
                 YOffset  =  - 50 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         [TestMethod] 
        public  void  ShouldAllowScrollingFromViewportByGivenAmountFromOrigin () 
         { 
             driver . Url  = 
                 "https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ; 
 
             var  scrollOrigin  =  new  WheelInputDevice . ScrollOrigin 
             { 
                 Viewport  =  true , 
                 XOffset  =  10 , 
                 YOffset  =  10 
             }; 
             new  Actions ( driver ) 
                 . ScrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . Perform (); 
 
             IWebElement  iframe  =  driver . FindElement ( By . TagName ( "iframe" )); 
             driver . SwitchTo (). Frame ( iframe ); 
             IWebElement  checkbox  =  driver . FindElement ( By . Name ( "scroll_checkbox" )); 
             Assert . IsTrue ( IsInViewport ( checkbox )); 
         } 
 
         private  bool  IsInViewport ( IWebElement  element ) 
         { 
             String  script  = 
                 "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n" 
                 +  "e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n" 
                 +  "return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n" 
                 +  "window.pageYOffset&&t+o>window.pageXOffset" ; 
             IJavaScriptExecutor  javascriptDriver  =  this . driver  as  IJavaScriptExecutor ; 
 
             return  ( bool ) javascriptDriver . ExecuteScript ( script ,  element ); 
         } 
     } 
 }     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform  /examples/ruby/spec/actions_api/wheel_spec.rb 
Copy
 
Close 
# frozen_string_literal: true 
 require  'spec_helper' 
 RSpec . describe  'Scrolling'  do 
  let ( :driver )  {  start_session  } 
 
   it  'scrolls to element'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . action 
           . scroll_to ( iframe ) 
           . perform 
 
     expect ( in_viewport? ( iframe )) . to  eq  true 
   end 
 
   it  'scrolls by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     delta_y  =  footer . rect . y 
     driver . action 
           . scroll_by ( 0 ,  delta_y ) 
           . perform 
 
     expect ( in_viewport? ( footer )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( iframe ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls from element by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html' ) 
 
     footer  =  driver . find_element ( tag_name :  'footer' ) 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . element ( footer ,  0 ,  - 50 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 
   it  'scrolls by given amount with offset'  do 
     driver . get ( 'https://selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html' ) 
 
     scroll_origin  =  Selenium :: WebDriver :: WheelActions :: ScrollOrigin . viewport ( 10 ,  10 ) 
     driver . action 
           . scroll_from ( scroll_origin ,  0 ,  200 ) 
           . perform 
 
     iframe  =  driver . find_element ( tag_name :  'iframe' ) 
     driver . switch_to . frame ( iframe ) 
     checkbox  =  driver . find_element ( name :  'scroll_checkbox' ) 
     expect ( in_viewport? ( checkbox )) . to  eq  true 
   end 
 end 
 def  in_viewport? ( element ) 
  in_viewport  =  <<~ IN_VIEWPORT 
     for ( var  e = arguments [ 0 ] , f = e . offsetTop , t = e . offsetLeft , o = e . offsetWidth , n = e . offsetHeight ; 
     e . offsetParent ;) f += ( e = e . offsetParent ) . offsetTop , t += e . offsetLeft ; 
     return  f < window . pageYOffset + window . innerHeight && t < window . pageXOffset + window . innerWidth && f + n > 
     window . pageYOffset && t + o > window . pageXOffset 
   IN_VIEWPORT 
 
   driver . execute_script ( in_viewport ,  element ) 
 end 
    await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 /examples/javascript/test/actionsApi/wheelTest.spec.js 
Copy
 
Close 
const  {  By ,  Browser ,  Builder }  =  require ( 'selenium-webdriver' ) 
const  assert  =  require ( 'assert' ) 
 describe ( 'Actions API - Wheel Tests' ,  function  ()  { 
  let  driver 
 
   before ( async  function  ()  { 
     driver  =  await  new  Builder (). forBrowser ( 'chrome' ). build (); 
   }) 
 
   after ( async ()  =>  await  driver . quit ()) 
 
   it ( 'Scroll to element' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  0 ,  iframe ) 
       . perform () 
     assert . ok ( await  inViewport ( iframe )) 
   }) 
 
   it ( 'Scroll by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
     const  deltaY  =  ( await  footer . getRect ()). y 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  deltaY ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
     assert . ok ( await  inViewport ( footer )) 
   }) 
 
   it ( 'Scroll from an element by a given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  0 ,  0 ,  200 ,  iframe ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an element with an offset' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
     const  footer  =  await  driver . findElement ( By . css ( "footer" )) 
 
     await  driver . actions () 
       . scroll ( 0 ,  - 50 ,  0 ,  200 ,  footer ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   it ( 'Scroll from an offset of origin (element) by given amount' ,  async  function  ()  { 
     await  driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
     const  iframe  =  await  driver . findElement ( By . css ( "iframe" )) 
 
     await  driver . actions () 
       . scroll ( 10 ,  10 ,  0 ,  200 ) 
       . perform () 
 
     await  driver . sleep ( 500 ) 
 
     await  driver . switchTo (). frame ( iframe ) 
     const  checkbox  =  await  driver . findElement ( By . name ( 'scroll_checkbox' )) 
     assert . ok ( await  inViewport ( checkbox )) 
   }) 
 
   function  inViewport ( element )  { 
     return  driver . executeScript ( "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\ne.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\nreturn f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\nwindow.pageYOffset&&t+o>window.pageXOffset" ,  element ) 
   } 
 })         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform ()  /examples/kotlin/src/test/kotlin/dev/selenium/actions_api/WheelTest.kt 
Copy
 
Close 
package  dev.selenium.actions_api 
 import  dev.selenium.BaseTest 
import  org.junit.jupiter.api.Assertions 
import  org.junit.jupiter.api.Test 
import  org.openqa.selenium.By 
import  org.openqa.selenium.JavascriptExecutor 
import  org.openqa.selenium.WebElement 
import  org.openqa.selenium.interactions.Actions 
import  org.openqa.selenium.interactions.WheelInput 
 class  WheelTest  :  BaseTest ()  { 
    
     @Test 
     fun  shouldScrollToElement ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         Actions ( driver ) 
                 . scrollToElement ( iframe ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( iframe )) 
     } 
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  deltaY  =  footer . getRect (). y 
         Actions ( driver ) 
                 . scrollByAmount ( 0 ,  deltaY ) 
                 . perform () 
 
         Assertions . assertTrue ( inViewport ( footer )) 
     }    
     
     @Test 
     fun  shouldScrollFromElementByGivenAmount ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( iframe ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     @Test 
     fun  shouldScrollFromElementByGivenAmountWithOffset ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html" ) 
 
         val  footer  =  driver . findElement ( By . tagName ( "footer" )) 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromElement ( footer ,  0 ,  - 50 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin , 0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     }     
     
     @Test 
     fun  shouldScrollFromViewportByGivenAmountFromOrigin ()  { 
         driver . get ( "https://www.selenium.dev/selenium/web/scrolling_tests/frame_with_nested_scrolling_frame.html" ) 
 
         val  scrollOrigin  =  WheelInput . ScrollOrigin . fromViewport ( 10 ,  10 ) 
         Actions ( driver ) 
                 . scrollFromOrigin ( scrollOrigin ,  0 ,  200 ) 
                 . perform () 
 
         val  iframe  =  driver . findElement ( By . tagName ( "iframe" )) 
         driver . switchTo (). frame ( iframe ) 
         val  checkbox  =  driver . findElement ( By . name ( "scroll_checkbox" )) 
 
         Assertions . assertTrue ( inViewport ( checkbox )) 
     } 
     
     fun  inViewport ( element :  WebElement ):  Boolean  { 
         val  script  =  "for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight; \n e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft; \n return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n> \n window.pageYOffset&&t+o>window.pageXOffset" 
         return  ( driver  as  JavascriptExecutor ). executeScript ( script ,  element )  as  Boolean  
     } 
 }