Search code examples
javascriptpythonseleniumselenium-webdrivermouseevent

how to triple-click on python to select a paragraph?


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

Solution

  • 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://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)