亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何三次點擊python來選擇一個段落?

如何三次點擊python來選擇一個段落?

繁花如伊 2023-04-25 15:37:33
有人請告訴我一種三次單擊 selenium python 的方法。我試過這個和其他東西,但沒有用。for x in range(3)    actions.click()
查看完整描述

5 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

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


查看完整回答
反對 回復 2023-04-25
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

這將三次單擊您在此頁面上提出的問題。希望能幫助到你。棘手的部分是 pyautogui 不關心瀏覽器窗口的位置。


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('https://stackoverflow.com/questions/63253535/how-to-triple-click-on-python-to-select-a-paragraph')

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)


查看完整回答
反對 回復 2023-04-25
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

嘗試找到兩個元素,然后將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。腳本選擇了中間的段落。所以應該沒問題。讓我知道


查看完整回答
反對 回復 2023-04-25
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您需要導入:


from selenium.webdriver.common.action_chains import ActionChains

然后你可以試試這個:


times = 3

while(times >0):

            ActionChains(driver).click().perform()

            times -= 1;


查看完整回答
反對 回復 2023-04-25
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

或者,您可以嘗試對其進行硬編碼。


actions = ActionChains(driver)


def triple_click(element_x):

    actions.click(element_x).click(element_x).click(element_x).perform()


triple_click(your_element)


查看完整回答
反對 回復 2023-04-25
  • 5 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號