how to triple-click on python to select a paragraph?
Asked Answered
E

6

3

Someone please tell me a way to triple-click on selenium python. I tried this and other things but it did not work.

for x in range(3)
   actions.click()
Equilibrium answered 4/8, 2020 at 19:5 Comment(1)
Don't forget to select the answer that solves your issue to help others with similar questions. See stackoverflow.com/help/someone-answersWinonawinonah
W
1

This will triple click on the question you asked on this page. Hope it helps. The tricky part is pyautogui does not care about where the browser window is.

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://mcmap.net/q/1925192/-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)
Winonawinonah answered 4/8, 2020 at 20:54 Comment(1)
Checkout Selenium Chat RoomIives
I
2

The current implementation of Selenium doesn't provide any way to perform a triple click. However a feasable approach would be to simulate the desired mouse events using execute_script() method as follows:

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")

Console Output:

Tripple click performed
Iives answered 4/8, 2020 at 21:11 Comment(3)
I like this strategy but the element passed to the function js_triple_click is never passed as an argument to the execute_script.Winonawinonah
This almost worked for me. It showed the bottom left url link bar to me but it did not click. Any way to fix this?Equilibrium
Help please...?Equilibrium
B
1

I would say that you are missing the perform action:

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')

Does it work now?

UPDATED ANSWER Try to locate two elements and then use drag_and_drop with those elements as source and end of the command. The code below seems to work and selects the paragraph.

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()

I used Wikipedia as a test and I took the xpath of two lines of text. The script selected the paragraph in between. So it should be okay. Let me know

Broiler answered 4/8, 2020 at 20:20 Comment(7)
It does a double-click instead.Equilibrium
@MoisesPark can you try with a while loop instead? check my answerStraightaway
@MoisesPark maybe you can try to set higher range. I tried on my side and I've got triple or multiple clicks.Broiler
Did it select a paragraph?Equilibrium
By doing a double-click i meant it selects a word only, not a paragraphEquilibrium
@MoisesPark Maybe try to import Keys and send Keys with CONTROL, 'a'. Something like element.sendKeys(Keys.CONTROL,'a')Broiler
@MoisesPark found it! Check this out, I updated my answer. Should do the trickBroiler
W
1

This will triple click on the question you asked on this page. Hope it helps. The tricky part is pyautogui does not care about where the browser window is.

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://mcmap.net/q/1925192/-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)
Winonawinonah answered 4/8, 2020 at 20:54 Comment(1)
Checkout Selenium Chat RoomIives
A
1

Alternatively, you can try hard coding it.

actions = ActionChains(driver)

def triple_click(element_x):
    actions.click(element_x).click(element_x).click(element_x).perform()

triple_click(your_element)
Ambidextrous answered 2/8, 2022 at 1:36 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ostraw
S
0

You need to import:

from selenium.webdriver.common.action_chains import ActionChains

Then you can try this:

times = 3
while(times >0):
            ActionChains(driver).click().perform()
            times -= 1;
Straightaway answered 4/8, 2020 at 20:51 Comment(1)
It does a double-click instead too. I think it has to do with selenium ActionChains.Equilibrium
S
0

If timing isn't an issue you could get away with:

actions = ActionChains(driver)
actions.double_click()
actions.click()
actions.perform()

Otherwise you can build your own w3c actions directly:

actions = ActionChains(driver)
actions.move_to_element(element)
actions.w3c_actions.pointer_action.click()
actions.w3c_actions.pointer_action.click()
actions.w3c_actions.pointer_action.click()
# don't forget to add a slight pause -- see actions.double_click()
for _ in range(4):
    actions.w3c_actions.key_action.pause()
actions.perform()
Soper answered 16/5, 2023 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.