11/08/2018, 19:29

Mouse actions trong Selenium

Thao tác với bàn phím trong Selenium Dưới đây là một số hành động chính của chuột mà các lập trình viên sẽ gặp phải trong hầu hết các ứng dụng: Click : Thực hiện nhấp chuột. Chúng tôi cũng có thể thực hiện một nhấp chuột dựa trên tọa độ. contextClick : Thực hiện ngữ cảnh ...

Thao tác với bàn phím trong Selenium

Dưới đây là một số hành động chính của chuột mà các lập trình viên sẽ gặp phải trong hầu hết các ứng dụng:

  • Click: Thực hiện nhấp chuột. Chúng tôi cũng có thể thực hiện một nhấp chuột dựa trên tọa độ.
  • contextClick: Thực hiện ngữ cảnh nhấp / nhấp chuột phải vào một phần tử hoặc dựa trên các tọa độ
  • doubleClick: Thực hiện nhấp đúp vào webelement hoặc dựa trên tọa độ. Nếu để trống, nó thực hiện nhấp đúp vào vị trí hiện tại.
  • moveToElement: Di chuyển chuột đến phần tử được chỉ định hoặc dựa trên tọa độ.

Dưới đây là cú pháp để gọi mouse actions trong Selenium bằng cách sử dụng Selenium WebDriver:

void click(WebElement onElement)
void contextClick(WebElement onElement)
void doubleClick(WebElement onElement)
void moveToElement(WebElement toElement)
void moveToElement(WebElement toElement, long xOffset, long yOffset)

Ví dụ

Dưới đây là ví dụ gọi mouse actions trong Selenium, mở trang web viettuts.vn, di chuyển chuột đến menu ‘web’, click vào link ‘JavaScript’.

package vn.viettuts.selenium;

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) {
        System.setProperty("webdriver.chrome.driver", 
                "D:SeleniumWebdriverchromedriver.exe");
        WebDriver driver = new ChromeDriver();

        // Open website
        driver.get("http://viettuts.vn");

        // Maximize the browser
        driver.manage().window().maximize();
        
        WebElement webMenu = driver.findElement(By.xpath(".//a[@href='/web']"));
        
        // move mouse to webMenu element
        Actions actions = new Actions(driver);
        actions.moveToElement(webMenu).perform();
    }
}

Kết quả:

Ví dụ Mouse actions trong Selenium
Thao tác với bàn phím trong Selenium
0