【问题标题】:Using Selenium to Click and Drag Across the Window to Highlight Text on the Web Page使用 Selenium 在窗口中单击并拖动以突出显示网页上的文本
【发布时间】:2022-06-11 08:47:39
【问题描述】:

我正在尝试将光标移动到一个元素上,然后在屏幕上单击并拖动以模拟某人在屏幕上突出显示文本。我的代码似乎并没有模拟这个动作。最初,我希望光标移动到指示的element,但这并没有发生。我没有看到光标移动,也没有突出显示。

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

【问题讨论】:

    标签: python html selenium-webdriver drag-and-drop selenium-chromedriver


    【解决方案1】:

    我遇到了这个确切的问题 - Selenium 具有“拖放”功能,但它实际上不适用于您的光标。最好使用 PyAutoGUI,这是一个可以物理控制鼠标和键盘的 Python 库。

    试试这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多