ActionChains.click_and_hold(element).perform()release()ActionChains(driver).click_and_hold(el).perform()# 快速参考 from selenium.webdriver.common.action_chains import ActionChains import time btn = driver.find_element(By.ID, "p5_btn") # 按住不放(不 release) ActionChains(driver).click_and_hold(btn).perform() time.sleep(1) # 观察加载效果
操作步骤:
① 用 driver.find_element(By.ID, "p5_btn") 定位「按住不放」按钮;
② 调用 ActionChains(driver).click_and_hold(btn).perform() 按住;
③ 等待 1 秒,断言按钮文字包含「加载中」或 p5_result 包含「按住」。
💡 提示:click_and_hold() 按住后不会自动释放,必须显式调用 release(),常用于拖拽的第一阶段。
# 练习 5 参考答案 · 按住 from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time driver.get("https://autotest-505.pages.dev/mouse_op_practice.html") btn = driver.find_element(By.ID, "p5_btn") actions = ActionChains(driver) # 按住不放(不释放) actions.click_and_hold(btn).perform() time.sleep(1) # 保持按住 1 秒观察状态 assert "加载中" in btn.text