有人请告诉我一种三次单击 selenium python 的方法。我尝试了这个和其他的东西,但没有奏效。
for x in range(3)
actions.click()
最佳答案
Selenium 的当前实现不提供任何方式来执行三次点击。然而,一种可行的方法是使用 execute_script()
模拟所需的鼠标事件。方法如下:
def js_triple_click(element, deltaY = 60, offsetX = 0, offsetY = 0):
driver.execute_script("""
"var target = arguments[0]; " +
"var offsetX = arguments[1]; " +
"var offsetY = arguments[2]; " +
"var rect = target.getBoundingClientRect(); " +
"var cx = rect.left + (offsetX || (rect.width / 2)); " +
"var cy = rect.top + (offsetY || (rect.height / 2)); " +
" " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
"emit('mouseup', {clientX: cx, clientY: cy}); " +
"emit('click', {clientX: cx, clientY: cy, detail: 3}); " +
" " +
"function emit(name, init) { " +
"target.dispatchEvent(new MouseEvent(name, init)); " +
"} " ;
""")
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.TAG_NAME, "p"))) # replace the locator as per your usecase
ActionChains(driver).move_to_element(element).perform()
js_triple_click(element)
print("Tripple click performed")
控制台输出:Tripple click performed
关于javascript - 如何在python上三次单击以选择一个段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63253535/