Search code examples
pythonhtmlselenium-webdriverdrag-and-dropselenium-chromedriver

Using Selenium to Click and Drag Across the Window to Highlight Text on the Web Page


I am trying to move the cursor to an element, then click and drag across the screen to simulate someone highlighting text on the screen. My code does not appear to be simulating this action though. Initially, I expect the cursor to move to the element indicated but this is not occurring. I don't see the cursor moving and nothing gets highlighted.

from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.action_chains import ActionChains
textreal_listing=[]
browser = webdriver.Chrome(r'\\homedirpva1a01\USERSNC$\603225\chromedriver\chromedriver.exe')
actions = ActionChains(browser)
time.sleep(5)


browser.get("https://www.nj.gov/dobi/division_insurance/bfd/enforcement2020.html")
time.sleep(5)
element = browser.find_element_by_xpath("/html/body/div/div/table[2]/tbody/tr/td/table/tbody/tr[2]/td[3]/table/tbody/tr[5]/td/p[1]/strong[1]")
time.sleep(5)
actions.move_to_element(element)
time.sleep(1)
actions.click_and_hold(on_element=None)
time.sleep(1)
actions.move_by_offset(550, 320).perform()
time.sleep(1)
actions.release()
actions.perform()

Solution

  • I was having this exact issue - Selenium has 'drag and drop' functionality but it doesn't physically apply to your cursor. The best thing to use for this is PyAutoGUI, a Python library that can take physical control of your mouse and keyboard.

    Try something like this:

    import pyautogui
    
    pyautogui.moveTo(element.location['x'], element.location['y'])
    pyautogui.dragTo(element.location['x'] + 550, element.location['y'] + 320)