Introducción: en las pruebas reales de productos web, la operación del mouse no es solo hacer clic (), sino que a veces también se hace clic con el botón derecho, doble clic, arrastrar y otras operaciones, que se incluyen en la clase ActionChains.
1. Métodos comunes de operación del mouse en ActionChains:
context_click (): haga clic con el botón derecho en
double_click (): haga doble clic en
drag_and_drop (): arrastre
move_to_element (): el mouse se mueve a un elemento
Ejemplos:
# cording = gbk
import os
from selenium import webdriver
from selenium.webdriver.common.by import By # 导入 by 方法
from selenium.webdriver.common.action_chains import ActionChains ## 对 鼠标 事件 操作
current_path = os.path.dirname (__ file__)
firefox_path = current_path + "/../ webdriver / geckodriver.exe"
driver = webdriver.Firefox (ejecutable_path = firefox_path)
driver.get ("http://127.0.0.1/zentao/user-login-L3plbnRhby9teS5odG1s.html")
mouse = ActionChains (driver) #Crear un objeto de mouse
# element1 = driver.find_element (By.XPATH, "// img [@src = '/ zentao / theme / default / images / main / zt-logo.png']" ) #Xpath usa el atributo para ubicar
element1 = driver.find_element (By.XPATH, "// img [contiene (@ src, 'images / main / zt-logo.png')]") #xpath usa el método incluir atributo para ubicar el
mouse. context_click (element1) .perform () #Ejecutar clic derecho del mouse, .perform () significa ejecución
element2 = driver.find_element (By.XPATH, "// button [@ type = 'button' y @ class = 'btn']") # Posicionamiento de atributos múltiples
mouse.move_to_element (element2) .perform () #Move a este elemento上#
Elementos de
captura de pantalla . Driver.find_element (By.XPATH, "// button [@ id = 'submit'] [@ type = 'submit']"). Captura de pantalla ('element1.png')