右クリックして、 Double クリックして Selenium
⚡ スマートサマリー
右クリックして、 Double クリックして Selenium マウス操作は Actions クラスによって自動化されます。このチュートリアルでは、両方の操作を実際に動作させて説明します。 Java コード、実際のテストシナリオ、およびそれらを駆動するメソッド 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デモページで右クリックし、表示されるコンテキストメニューからオプションを選択します。
テストシナリオ:
- 打ち上げ: https://demo.guru99.com/test/simple_context_menu.html
- 「右クリックしてください」ボタンを右クリックします。
- 表示されたメニューの「編集」リンクをクリックしてください。
- 警告メッセージが表示されたら「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;
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();
}
}
結果: コンテキストメニューが表示され、「編集」オプションが選択されます。
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 コンソール。



