如何三次点击 python 到 select 一个段落?
how to triple-click on python to select a paragraph?
有人请告诉我一种在 selenium 上三次单击的方法 python。我尝试了这个和其他东西,但没有用。
for x in range(3)
actions.click()
我会说你错过了执行动作:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
actions = ActionChains(driver)
for i in range(3):
actions.click()
actions.perform()
print('click')
现在可以用了吗?
更新的答案
尝试找到两个元素,然后使用 drag_and_drop 将这些元素作为命令的源和结尾。下面的代码似乎有效并选择了段落。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Home")
actions = ActionChains(driver)
# first element and last element in the paragraph
start = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/div[1]')
end = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/div[4]')
actions.drag_and_drop(start, end).perform()
我用维基百科做测试,我取了两行文字的xpath。脚本选择了中间的段落。所以应该没问题。让我知道
您需要导入:
from selenium.webdriver.common.action_chains import ActionChains
那你可以试试这个:
times = 3
while(times >0):
ActionChains(driver).click().perform()
times -= 1;
这将三次点击您在此页面上提出的问题。希望能帮助到你。棘手的部分是 pyautogui 不关心浏览器 window 在哪里。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pyautogui
driver = webdriver.Firefox(executable_path=r'C:\Path\To\Your\geckodriver.exe')
driver.get('
driver.maximize_window()
test_paragraph = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//p[contains(text(), 'Someone please tell me a way to triple-click ')]")))
# import time
# time.sleep(3)
panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
abs_x = test_paragraph.location['x']
y = test_paragraph.location['y']
abs_y = y + panel_height
print("Absolute x : " + str(abs_x))
print("Absolute y : " + str(abs_y))
pyautogui.moveTo(abs_x + 10, abs_y)
pyautogui.click(clicks=3)
的当前实现不提供任何执行三次点击的方法。然而,一种可行的方法是使用 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
有人请告诉我一种在 selenium 上三次单击的方法 python。我尝试了这个和其他东西,但没有用。
for x in range(3)
actions.click()
我会说你错过了执行动作:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
actions = ActionChains(driver)
for i in range(3):
actions.click()
actions.perform()
print('click')
现在可以用了吗?
更新的答案 尝试找到两个元素,然后使用 drag_and_drop 将这些元素作为命令的源和结尾。下面的代码似乎有效并选择了段落。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Home")
actions = ActionChains(driver)
# first element and last element in the paragraph
start = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/div[1]')
end = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/div[4]')
actions.drag_and_drop(start, end).perform()
我用维基百科做测试,我取了两行文字的xpath。脚本选择了中间的段落。所以应该没问题。让我知道
您需要导入:
from selenium.webdriver.common.action_chains import ActionChains
那你可以试试这个:
times = 3
while(times >0):
ActionChains(driver).click().perform()
times -= 1;
这将三次点击您在此页面上提出的问题。希望能帮助到你。棘手的部分是 pyautogui 不关心浏览器 window 在哪里。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pyautogui
driver = webdriver.Firefox(executable_path=r'C:\Path\To\Your\geckodriver.exe')
driver.get('
driver.maximize_window()
test_paragraph = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//p[contains(text(), 'Someone please tell me a way to triple-click ')]")))
# import time
# time.sleep(3)
panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;')
abs_x = test_paragraph.location['x']
y = test_paragraph.location['y']
abs_y = y + panel_height
print("Absolute x : " + str(abs_x))
print("Absolute y : " + str(abs_y))
pyautogui.moveTo(abs_x + 10, abs_y)
pyautogui.click(clicks=3)
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