右クリックして、 Double クリックして Selenium

⚡ スマートサマリー

右クリックして、 Double クリックして Selenium マウス操作は Actions クラスによって自動化されます。このチュートリアルでは、両方の操作を実際に動作させて説明します。 Java コード、実際のテストシナリオ、およびそれらを駆動するメソッド Selenium ウェブドライバー。

  • 🖱️ コアメソッド: Actionsクラスは、任意のWebElement上で右クリックするためのcontextClick()とダブルクリックするためのdoubleClick()を提供します。
  • 🧭 ワークフローパターン: 要素を特定し、ドライバを使用してアクションをインスタンス化し、アクションを呼び出し、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. 警告メッセージが表示されたら「OK」をクリックします。
  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スクリプトがアラートを発し、プログラムによってそれを確認します。

テストシナリオ:

  • 打ち上げ: https://demo.guru99.com/test/simple_context_menu.html
  • Double 「ボタンをクリックしてください」Double「アラートを見るにはここをクリック」
  • 警告メッセージが表示されたら「OK」をクリックします。

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() は実際の右クリックをシミュレートし、アプリケーションのコンテキストメニューを開きます。この 2 つの用語は、 Selenium ドキュメント。

Actions クラスはイベントをキューに格納します。perform() メソッドが呼び出されない場合、連鎖したステップはディスパッチされません。perform() メソッドはキューをフラッシュし、キューに格納された各アクションを順番に実行します。

はい、 Javaスクリプトエグゼキュータはダブルクリックを発生させることはできますが、実際のブラウザイベントをバイパスします。アクションを使用する方法が推奨されます。 Selenium 標準。

はい。アクションによる右クリックとダブルクリックはヘッドレス Chrome で機能し、 Firefox W3C Actions APIがサポートされていることが前提です。 Selenium 4人のドライバーがこれを完全にサポートしています。

アクションクラスが到着しました Selenium WebDriver 2.0 は org.openqa.selenium.interactions 内にあり、 Selenium 3と4はW3Cアクションをサポートしています。

AIツールと Selenium 選択肢 クリック時の要素変更に適応する自己修復型ロケーターを適用することで、動的なIDやレイアウトの変動による不安定な不具合を軽減します。

はい。AIコードアシスタントは、平易な英語のシナリオを Selenium Java or Python contextClick() と doubleClick() を使用したコード、および堅牢なロケーターと待機処理の提案。