Web automation simulates mouse and keyboard operation

1. Mouse operation

1.1 Mouse hover operation, move_to_element

from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 

action = ActionChains (driver) 
#instantiate mouse class action.move_to_element ("Element"). perform () # hover operation, call .perform () to perform mouse operation

  

1.2. Mouse drag operation

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains (driver) #Instantiate mouse class 
action.drag_and_drop ("Element"). perform () # Drag operation, call .perform () to perform mouse operation

  

1.3. Right mouse button operation

from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 

action = ActionChains (driver) 
#instantiate mouse class action.context_click ("element"). perform () # hover operation, call .perform () to perform mouse operation

  

1.4. Left mouse button operation

from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 

action = ActionChains (driver) #instantiate mouse class 
action.double_click ("element"). perform () # hover operation, call .perform () to perform mouse operation

 

2. Keyboard operation

2.1 Mouse operation, first import the library, call send_keys () to complete the operation

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Chrome () 

driver.get ("htttp: //www.baidu.com") 

driver.find_element_by_id ('kw'). send_keys ("test ") 

driver.find_element_by_id ('kw'). send_keys (Keys.ENTER) #Use the enter key of the mouse to initiate the operation

 

2.2 Key combination

send_keys(Keys.CONTROL,'a')#全选(ctrl+A)

send_keys (Keys.CONTROL, 'c') # Copy (ctrl + C)

send_keys(Keys.CONTROL,'x')#剪切(ctrl+X)

send_keys (Keys.CONTROL, 'v') # Paste (ctrl + V)

 

2.3 Non-key combinations

Enter key: send_keys (Keys.ENTER)

Delete key: send_keys (Keys.BACK_SPACE)

Spacebar: send_keys (Keys.SPACE)

Tab key: send_keys (Keys.TAB)

Back key: send_keys (Keys.ESCAPE)

Refresh key: send_keys (Keys.F5)

 

 

 

Guess you like

Origin www.cnblogs.com/hherbk/p/12700359.html