演示 Selenium ActionChains 中最基础也是最常用的 click() 左击操作。
两种点击方式对比:
el.click() → WebElement 方法,内部自动滚到可见 + 等待可点击,推荐日常用ActionChains(driver).click(el).perform() → 链式操作,适合和 hover / 拖拽 / 按键等其他鼠标动作组合;click(el) 会自动先 move_to_element(el),换成无参 click() 则点"当前鼠标位置"(初始坐标 (0, 0),即视口左上角),需要前面先链 move_to_element / move_to_element_with_offset 等把鼠标送到位⚠️ 常见坑:元素被遮挡时 click() 会抛 ElementClickInterceptedException,此时用 execute_script('arguments[0].click()', el) 走 JS 点击可以绕过遮挡检测,但可能触发不了正常的事件冒泡。
💡 实战经验:90% 场景用 el.click() 就够了,只有需要组合拖拽/悬停/双击时才用 ActionChains。
element.click() —— 最简单,直接在元素上调用ActionChains(driver).click(element).perform() —— 适合和 hover / 拖拽 / 按键等其他动作链式组合;
click(el) 会自动先 move_to_element(el),换成无参 click() 则点"当前鼠标位置"(初始坐标 (0, 0),即视口左上角),需要前面先链 move_to_element / move_to_element_with_offset 等把鼠标送到位
el.click() 或 ActionChains(driver).click(el).perform()
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains # 1. 启动浏览器 + 打开演示页面 driver = webdriver.Chrome() driver.get("https://autotest-505.pages.dev/mouse_op_tech_sections/mouse_click_tech.html") # 2. 定位元素 counter = driver.find_element(By.ID, "clickCounter") # 方式①:直接 click(最常用) counter.click() # 方式②:ActionChains.click(可链式组合) # click(el) 内部会先自动 move_to_element(el),再无参 click() 点当前位置 actions = ActionChains(driver) actions.click(counter).perform() # 方式②的等价写法:显式链 move_to_element + 无参 click() actions.move_to_element(counter).click().perform() # 3. 操作完成后关闭浏览器 driver.quit()