Selenium is commonly used API - simulated mouse

Learned through the previous example, you can use the click () to simulate a mouse click operation, and now the Web product provides a richer mouse interaction, such as a mouse right-click, double click, hover, or even a mouse drag functions . In WebDriver, the mouse on these methods provide a packaging operation in ActionChains class.
Actions class provides a common method of operation of the mouse:

  • contextClick () Right-click
  • clickAndHold () mouse click and control
  • doubleClick () Double-click on
  • dragAndDrop () drag
  • release () Release the mouse
  • perform () performs all stored behavior Actions

Baidu Home Settings hover drop-down menu.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
 
public class MouseDemo {
 
  public static void main(String[] args) {
 
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.baidu.com/");
 
    WebElement search_setting = driver.findElement(By.linkText("设置"));
    Actions action = new Actions(driver);
    action.clickAndHold(search_setting).perform();
 
    driver.quit();
  }
}
  • import org.openqa.selenium.interactions.Actions;

Introducing the mouse class provides ActionChains

  • Actions (driver) calls the Actions () class, the browser driver driver as a parameter.
  • clickAndHold () method used to simulate mouse hovering operation, need to specify the elements positioned in the call.
  • perform () performs all acts ActionChains stored, will be understood to be submitted to the operation of the entire operation.

1. Other methods of operation on the mouse

import org.openqa.selenium.interactions.Actions;
……
 
Actions action = new Actions(driver);
 
// 鼠标右键点击指定的元素
action.contextClick(driver.findElement(By.id("element"))).perform();
 
// 鼠标右键点击指定的元素
action.doubleClick(driver.findElement(By.id("element"))).perform();
 
// 鼠标拖拽动作, 将 source 元素拖放到 target 元素的位置。
WebElement source = driver.findElement(By.name("element"));
WebElement target = driver.findElement(By.name("element"));
action.dragAndDrop(source,target).perform();
 
// 释放鼠标
action.release().perform();

Guess you like

Origin www.cnblogs.com/zhizhao/p/11303186.html