คลิกขวาและ Double คลิกเข้า Selenium
⚡ สรุปอย่างชาญฉลาด
คลิกขวาและ Double คลิกเข้า Selenium การกระทำของเมาส์จะถูกทำให้เป็นอัตโนมัติผ่านคลาส Actions หรือไม่ บทช่วยสอนนี้จะสาธิตการทำงานทั้งสองแบบพร้อมตัวอย่างการใช้งานจริง Java โค้ด สถานการณ์ทดสอบจริง และวิธีการที่ขับเคลื่อนสถานการณ์เหล่านั้นภายใน Selenium เว็บไดร์เวอร์

คลิกขวาเข้าไป Selenium
คลิกขวาที่การกระทำใน Selenium การทำงานของ WebDriver นั้นใช้คลาส Actions การดำเนินการนี้เรียกอีกอย่างว่า Context Click โดยมีการกำหนดค่าไว้ล่วงหน้าแล้ว contextClick() เมธอดของคลาส Actions ทำหน้าที่คลิกขวา ด้านล่างนี้คือไวยากรณ์พื้นฐาน
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();
วิธีคลิกขวาเข้า Selenium
สถานการณ์ด้านล่างนี้จะเริ่มต้นขึ้น Guruหน้าสาธิต 99 คลิกขวา แล้วเลือกตัวเลือกจากเมนูบริบทที่ปรากฏขึ้น
สถานการณ์การทดสอบ:
- เปิด: https://demo.guru99.com/test/simple_context_menu.html
- คลิกขวาที่ปุ่ม “คลิกขวาที่ฉัน”
- คลิกที่ลิงก์ แก้ไข ในเมนูที่แสดง
- คลิกตกลงในข้อความแจ้งเตือน
- ปิดเบราว์เซอร์
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 โดยใช้ instance ของ driver
- ค้นหาองค์ประกอบเป้าหมายด้วย
findElement. - โทร
doubleClick()และโซ่perform()เพื่อดำเนินการ
วิธีการ Double คลิกเข้า Selenium
สถานการณ์ต่อไปนี้แสดงให้เห็นถึงการดับเบิ้ลคลิกแบบเต็มรูปแบบซึ่งจะเรียกใช้งาน... Javaสคริปต์จะแจ้งเตือนและยืนยันการแจ้งเตือนนั้นโดยอัตโนมัติ
สถานการณ์การทดสอบ:
- เปิด: https://demo.guru99.com/test/simple_context_menu.html
- Double คลิกปุ่ม “Double-คลิกที่นี่เพื่อดูการแจ้งเตือน"
- คลิกตกลงในข้อความแจ้งเตือน
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 ปลอบใจ



