在 Selenium 中执行鼠标操作 Python

Perform mouse actions in Selenium Python

我有以下脚本:

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 time

url = 'https://www.icribis.com/it/'

codes = [...] # my codes

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)

time.sleep(0.5)

# Disable cookies
driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")').click()

time.sleep(0.5)

for code in codes:
    # Select Codice fiscale (= fiscal code)
    wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='search-type-fiscal-code']"))).click()
    time.sleep(0.5)

    # Clean the search bar
    driver.find_element(by=By.ID, value='companySearchFormInput').clear()
    time.sleep(0.5)

    # Insert the fiscal code in the search bar
    wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@name='search']"))).send_keys(code)
    time.sleep(0.5)

    # Click on the button
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='companySearch']//input[@type='submit']"))).click()
    time.sleep(0.5)

    # Rest of the code

driver.close()

在 Selenium Python 中有没有一种方法可以使用鼠标执行上述某些操作?

例如,在# Select Codice fiscale (= fiscal code)的情况下,将鼠标向上移动到单词“Codice fiscale”(在它的任何一点)上,然后单击(select)它?

提前感谢您的澄清。

是的,可以将鼠标向上移动到“Codice fiscale”元素上,但这会涉及更多代码行,并且可能会导致不必要的复杂性。

相反,使用 expected_conditions like and 可以使您的工作更加轻松和简单。


操作数API

Actions API 是 low-level 接口,用于向网络浏览器提供虚拟化设备输入。与执行额外验证的 high-level 元素交互不同,操作 API 提供对输入设备的精细控制。 Selenium 提供对 3 个输入源的访问,如下所示:


鼠标操作 APIs

鼠标操作的几个例子如下:

  • 点击并按住:移动到元素并在给定元素中间点击(不松开)

  • 上下文点击:该方法首先对元素所在位置执行mouse-move,然后执行context-click(右击) 在给定元素上。

  • 双击: 移动到元素并在给定元素中间执行double-click

  • 移动到元素:该方法将鼠标移动到元素的中间。该元素也会在执行此操作时滚动到视图中..

    from selenium import webdriver
    driver = webdriver.Chrome()
    
    # Navigate to url
    driver.get("http://www.google.com")
    
    # Store 'google search' button web element
    gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
    
    # Performs mouse move action onto the element
    webdriver.ActionChains(driver).move_to_element(gmailLink).perform()
    
  • 按偏移量移动:此方法将鼠标从其当前位置(或 0,0)移动给定的偏移量。如果坐标在视图之外window,那么鼠标最终会在浏览器之外window。

    from selenium import webdriver
    driver = webdriver.Chrome()
    
    # Navigate to url
    driver.get("http://www.google.com")
    
    # Store 'google search' button web element
    gmailLink = driver.find_element(By.LINK_TEXT, "Gmail")
    # Set x and y offset positions of element
    xOffset = 100
    yOffset = 100
    # Performs mouse move action onto the element
    webdriver.ActionChains(driver).move_by_offset(xOffset,yOffset).perform()
    
  • dragAndDrop:该方法首先对源元素执行一次click-and-hold,移动到目标元素所在位置,然后松开鼠标.

  • dragAndDropBy:该方法首先对源元素执行click-and-hold,移动到给定的偏移量,然后释放鼠标。

  • release:这个动作释放按下的鼠标左键。如果传递 WebElement,它将释放给定 WebElement

    上按下的鼠标左键