Selenium mouse click
Mouse clicks can be divided into
- Click click()
- Double click double_click()
- Right click context_click()
- Click the left mouse button without releasing click_and_hold()
Now take the example of Baidu as an example
from selenium import webdriver
from selenium.webdriver.common.by import By
drive=webdriver.Chrome()
drive.get('https://www.baidu.com/')
SubmitBut=drive.find_element(By.XPATH,'//input[@type="submit" and @value="百度一下"]')
SubmitBut.click()
Now use the mouse
1. Import the relevant library
2. Create the mouse object
3. Implement the relevant operation
Now we use the code to implement such a function (right click on the Baidu search tab)
code show as below
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains#####
drive=webdriver.Chrome()
drive.get('https://www.baidu.com/')
SubmitBut=drive.find_element(By.XPATH,'//input[@type="submit" and @value="百度一下"]')
action_mouse=ActionChains(drive)######
action_mouse.move_to_element(SubmitBut).context_click().perform()######
Get it done
Mouse movement
drag_and_drop(A, B) drag A to B
drag_and_drop_by_offset(source, x, y) Drag A to a certain position
move_by_offset(x, y) Move the mouse from the current position to a certain coordinate
move_to_element(to_element) Move the mouse to an element
move_to_element_with_offset(A, x, y) to move to the position of the distance from the A element (the upper left corner coordinate)
perform() performs all actions in the chain
release(on_element=None) Release the left mouse button at an element position (used with clic_and_hold())
send_keys('python') Send'python' through the keyboard to input characters and simulate pressing the letter keys
send_keys_to_element(At,'python') send'python' to A
Here are a few examples, the other methods of operation are the same
(because I can't find the corresponding website to give an example, now use pseudo code to demonstrate, and write an example later.)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('URL')
dragger = driver.find_element_by_id('dragger') # 被拖拽元素
A = driver.find_element_by_xpath('') # 目标A
B = driver.find_element_by_xpath('') # 目标B
action = ActionChains(driver)
action.drag_and_drop(dragger, A).perform() # 移动dragger到目标A
action.click_and_hold(dragger).release(A).perform() # 效果与上句相同,也能起到移动效果
action.click_and_hold(dragger).move_to_element(A).release().perform() # 效果与上两句相同,也能起到移动的效果
# action.drag_and_drop_by_offset(dragger, 400, 150).perform() # 4.移动到指定坐标
action.click_and_hold(dragger).move_by_offset(400, 150).release().perform() #与上一句相同,移动到指定坐标
Frequently used alert
1. drag_and_drag_by_offset (A, x, y) is generally used for slider verification.
2. The origin coordinate of move_by_offset (x, y) is that the end point of the previous move is the origin of the next time, that is, the coordinates have an additive effect.
Keyboard events
This is combined with the mouse, and sometimes you need to use certain keys such as the'space' key.
key_down(value, element=None) Press a key on a keyboard
key_up(value, element=None) release a key
The following is a specific example. (Fake code)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('URL')
dragger = driver.find_element_by_id('dragger') # 被拖拽元素
A = driver.find_element_by_xpath('') # 目标A
INPUT = driver.find_element_by_xpath('') # 目标B
action = ActionChains(driver)
action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform() # ctrl+a
action.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform() # ctrl+c
action.key_down(Keys.CONTROL,INPUT).send_keys('v').key_up(Keys.CONTROL).perform() # ctrl+v
action.move_to_element(INPUT).key_down(Keys.CONTROL).send_keys('v')key_up(Keys.CONTROL).perform()#和上一个一样 ctrl+v
Real example
The life and death sniper landing example (don’t laugh seriously)
Let’s demonstrate how to get the coordinates
code show as below;
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome()
driver.maximize_window()
enter_url='https://ssjj.4399.com/'
driver.get(enter_url)
actions=ActionChains(driver)
actions.move_by_offset(550,370).click().send_keys('865989840')
actions.move_by_offset(-550,-370)#归位
actions.move_by_offset(550,400).click().send_keys('mima')
actions.move_by_offset(-550,-400)
actions.move_by_offset(530,475).click()
actions.move_by_offset(-530,-475).perform()
actions.move_by_offset(400,350).click().perform()
Effect
(I signed it by the way. Chrome does not support flash now. If there is no special modification, it cannot run flash automatically. Of course, you can modify it. Search some WeChat public accounts for tutorials, which are not explained in this building. )