慕哥6287543
要构建多行脚本,您可以借助三重引号ie """ ... """。这是通过execute_script()使用Selenium调用的多行脚本示例:def wheel_element(element, deltaY = 120, offsetX = 0, offsetY = 0): error = element._parent.execute_script(""" var element = arguments[0]; var deltaY = arguments[1]; var box = element.getBoundingClientRect(); var clientX = box.left + (arguments[2] || box.width / 2); var clientY = box.top + (arguments[3] || box.height / 2); var target = element.ownerDocument.elementFromPoint(clientX, clientY); for (var e = target; e; e = e.parentElement) { if (e === element) { target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY})); target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY})); target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY})); return; } } return "Element is not interactable"; """, element, deltaY, offsetX, offsetY)您可以将方法调用为:wheel_element(elm, -120)