마우스 오른쪽 버튼을 클릭하고 Double 클릭 Selenium

⚡ 스마트 요약

마우스 오른쪽 버튼을 클릭하고 Double 클릭 Selenium 마우스 동작은 Actions 클래스를 통해 자동화됩니다. 이 튜토리얼에서는 두 가지 동작 모두 작동하는 모습을 보여줍니다. Java 코드, 실제 테스트 시나리오, 그리고 그것들을 구동하는 방법론 Selenium 웹드라이버.

  • 🖱️ 핵심 방법: Actions 클래스는 모든 WebElement에 대해 마우스 오른쪽 클릭 시 contextClick() 메서드와 더블 클릭 시 doubleClick() 메서드를 제공합니다.
  • 🧭 워크플로우 패턴: 해당 요소를 찾고, 드라이버를 사용하여 Actions 인스턴스를 생성하고, 액션을 호출하고, perform()을 연결하여 실행합니다.
  • 🧪 테스트 시나리오: 두 예제 모두 검증되고 반복 가능한 결과를 얻기 위해 demo.guru99.com/test/simple_context_menu.html을 대상으로 합니다.
  • 🆚 행동 구분: 마우스 오른쪽 버튼을 클릭하면 상황별 메뉴가 열리고, 두 번 클릭하면 알림이나 편집과 같은 상태 변경이 실행됩니다.
  • 🤖 AI 통합: 자가 복구 위치 추적기 및 AI 지원 Selenium 프레임워크는 클릭 동작 중 발생하는 불안정성을 줄여줍니다.

마우스 오른쪽 버튼을 클릭하고 Double 클릭 Selenium

마우스 오른쪽 버튼을 클릭하세요. Selenium

오른쪽 클릭 동작 Selenium WebDriver는 Actions 클래스를 사용하여 구현됩니다. 이 작업은 컨텍스트 클릭이라고도 합니다. 미리 정의된 contextClick() Actions 클래스의 메서드는 마우스 오른쪽 클릭을 수행합니다. 기본 구문은 다음과 같습니다.

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();

마우스 오른쪽 버튼을 클릭하는 방법 Selenium

아래 시나리오는 다음을 시작합니다. Guru99 데모 페이지에서 마우스 오른쪽 버튼을 클릭하고 나타나는 컨텍스트 메뉴에서 옵션을 선택합니다.

테스트 시나리오 :

  1. 쏘다: https://demo.guru99.com/test/simple_context_menu.html
  2. "오른쪽 클릭" 버튼을 마우스 오른쪽 버튼으로 클릭하세요.
  3. 표시된 메뉴에서 편집 링크를 클릭하세요.
  4. 알림창에서 확인을 클릭하세요.
  5. 브라우저를 닫습니다

Code:

package test;
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 ContextClick {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
        driver = new ChromeDriver();

        driver.get("https://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();

        Actions action = new Actions(driver);
        WebElement link = driver.findElement(By.cssSelector(".context-menu-one"));
        action.contextClick(link).perform();

        WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy"));
        element.click();
    }
}

결과 : 컨텍스트 메뉴가 나타나고 [편집] 옵션이 선택됩니다.

마우스 오른쪽 버튼을 클릭하세요. Selenium

Double 클릭 Selenium

동일한 행동 기반 패턴을 따르면, Double 클릭 Selenium WebDriver는 미리 정의된 것을 사용합니다. doubleClick() 메서드입니다. Actions 클래스는 마우스 오른쪽 클릭, 드래그 앤 드롭, 마우스 오버와 같은 복합적인 마우스 및 키보드 작업을 위한 표준 도우미 클래스입니다.

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.doubleClick(elementLocator).perform();

실행 흐름은 다음과 같습니다.

  • 드라이버 인스턴스를 사용하여 Actions 객체를 인스턴스화합니다.
  • 대상 요소를 찾으세요 findElement.
  • 상담 예약 번호 doubleClick() 그리고 체인 perform() 실행하다.

방법 Double 클릭 Selenium

다음 시나리오는 더블 클릭이 트리거되는 상황을 보여줍니다. Java스크립트는 경고를 표시하고 프로그램적으로 이를 확인합니다.

테스트 시나리오 :

Code:

package test;
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;
import org.openqa.selenium.Alert;

public class DoubleClickDemo {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
        driver = new ChromeDriver();

        driver.get("https://demo.guru99.com/test/simple_context_menu.html");
        driver.manage().window().maximize();

        Actions action = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
        action.doubleClick(link).perform();

        Alert alert = driver.switchTo().alert();
        System.out.println("Alert Text\n" + alert.getText());
        alert.accept();
    }
}

결과 : 알림창이 뜨고 알림 내용이 출력됩니다. Eclipse 콘솔.

Double 클릭 Selenium 경보

Double 콘솔 출력을 클릭하세요

자주 묻는 질문

WebElement.click()은 왼쪽 클릭만 발생시킵니다. 오른쪽 클릭과 더블 클릭은 Actions 클래스의 perform() 메서드를 통해 연결하고 전달해야 하는 복합 마우스 이벤트입니다.

네. contextClick() 함수는 실제 마우스 오른쪽 클릭을 시뮬레이션하여 애플리케이션의 컨텍스트 메뉴를 엽니다. 두 용어는 애플리케이션에서 같은 의미로 사용됩니다. Selenium 문서.

Actions 클래스는 이벤트를 큐에 저장합니다. perform() 메서드를 호출하지 않으면 연결된 단계들이 실행되지 않습니다. perform() 메서드는 큐를 비우고 큐에 있는 각 액션을 순서대로 실행합니다.

네! Java스크립트 실행기는 더블 클릭 이벤트를 발생시킬 수 있지만, 실제 브라우저 이벤트를 무시합니다. 액션을 사용하는 방식이 권장됩니다. Selenium 표준.

예. 마우스 오른쪽 클릭과 더블 클릭은 액션 기능을 통해 헤드리스 크롬에서도 작동합니다. Firefox W3C Actions API가 지원되는 경우에 한합니다. Selenium 4개의 드라이버가 이를 완벽하게 지원합니다.

Actions 클래스가 도착했습니다. Selenium WebDriver 2.0은 org.openqa.selenium.interactions 내부에 있으며 유지됩니다. Selenium W3C 액션을 지원하는 3 및 4 버전입니다.

AI 도구 및 Selenium 대안 클릭 중 요소 변경에 맞춰 조정되는 자체 복구 로케이터를 적용하여 동적 ID 또는 레이아웃 변경으로 인한 불안정한 오류를 줄입니다.

예. AI 코드 도우미는 일반적인 영어 시나리오를 AI 코드로 변환할 수 있습니다. Selenium Java or Python contextClick() 및 doubleClick()을 사용하는 코드와 함께, 안정적인 위치 지정 및 대기 방법을 제안합니다.

이 게시물을 요약하면 다음과 같습니다.