Foreplay
When we are doing automation, we sometimes use some mouse operations, such as double-clicking, mouse hovering and other operations. Selenium provides the ActionChains method for us to operate< /p>
Double click
from selenium import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# Assign the located search box to the variable
choose = driver.find_element_by_css_selector('#kw')
# Double click with the mouse
ActionChains(driver).double_click(choose).perform()
sleep(2)
driver.quit()
right mouse button
from selenium import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# Assign the located search box to the variable
choose = driver.find_element_by_css_selector('#kw')
# Double click with the mouse
ActionChains(driver).double_click(choose).perform()
sleep(2)
# Right mouse button
ActionChains(driver).context_click(choose).perform()
sleep(2)
driver.quit()
Mouse hover
from selenium import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# locate class=pf and assign it to the variable move
move = driver.find_element_by_css_selector('.pf')
# mouse hover
ActionChains(driver).move_to_element(move).perform()
sleep(2)
driver.quit()
from selenium < span style="color: #0000ff;">import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# Assign the located search box to the variable
choose = driver.find_element_by_css_selector('#kw')
# Double click with the mouse
ActionChains(driver).double_click(choose).perform()
sleep(2)
driver.quit()
from selenium import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# Assign the located search box to the variable
choose = driver.find_element_by_css_selector('#kw')
# Double click with the mouse
ActionChains(driver).double_click(choose).perform()
sleep(2)
# Right mouse button
ActionChains(driver).context_click(choose).perform()
sleep(2)
driver.quit()
from selenium import webdriver
from selenium.webdriver.common.action_chains import< span style="color: #000000;"> ActionChains
from time import sleep
driver = webdriver.Chrome()
driver.get('http://www.baidu.com ')
driver.maximize_window()
driver.find_element_by_css_selector('#kw').send_keys('python span>')
sleep(2)
# locate class=pf and assign it to the variable move
move = driver.find_element_by_css_selector('.pf')
# mouse hover
ActionChains(driver).move_to_element(move).perform()
sleep(2)
driver.quit()