คลิกขวาและ Double คลิกเข้า Selenium (ตัวอย่าง)
คลิกขวาเข้า Selenium
คลิกขวาที่การกระทำใน Selenium ไดรเวอร์เว็บสามารถทำได้โดยใช้คลาส Actions การดำเนินการคลิกขวาเรียกอีกอย่างว่า Context Click ใน Seleniumบริบทของวิธีการที่กำหนดไว้ล่วงหน้าที่จัดทำโดยคลาส Actions ใช้เพื่อดำเนินการคลิกขวา ด้านล่างนี้คือโค้ดเพื่อสาธิตการดำเนินการคลิกขวาโดยใช้คลาส Actions
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();
วิธีคลิกขวาเข้า Selenium
สถานการณ์การทดสอบ:
- เปิดตัว URL: https://demo.guru99.com/test/simple_context_menu.html
- ทำการคลิกขวาที่ปุ่ม : คลิกขวาที่ฉัน
- คลิกที่ลิงก์แก้ไขในรายการตัวเลือกคลิกขวาที่แสดง
- คลิกปุ่มตกลงบนการแจ้งเตือนที่แสดง
- ปิดเบราว์เซอร์
รหัส:
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();
//Launch the Application Under Test (AUT)
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
// Right click the button to launch right click menu options
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.cssSelector(".context-menu-one"));
action.contextClick(link).perform();
// Click on Edit link on the displayed menu options
WebElement element = driver.findElement(By.cssSelector(".context-menu-icon-copy"));
element.click();
// Accept the alert displayed
//driver.switchTo().alert().accept();
// Closing the driver instance
//driver.quit();
}
}
ผลลัพธ์:
Double คลิกเข้า Selenium
Double คลิกการกระทำใน Selenium ไดรเวอร์เว็บสามารถทำได้โดยใช้คลาส Actions คลาส Actions เป็นคลาสที่กำหนดไว้ล่วงหน้าใน Selenium ไดรเวอร์เว็บที่ใช้สำหรับการดำเนินการคีย์บอร์ดและเมาส์หลายอย่าง เช่น คลิกขวา ลากและวาง เป็นต้น
Double คลิกเข้า Selenium โดยใช้คลาส Actions
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.doubleClick(elementLocator).perform();
- เริ่มแรก เราจำเป็นต้องสร้างอินสแตนซ์อ็อบเจ็กต์ของคลาส Actions โดยส่งอินสแตนซ์ไดรเวอร์เป็นพารามิเตอร์
- การใช้คำสั่งค้นหาองค์ประกอบ เราต้องค้นหาตำแหน่งขององค์ประกอบที่เราต้องการดับเบิลคลิก
- โดยใช้การดำเนินการดับเบิลคลิกที่กำหนดไว้ล่วงหน้าของคลาส Actions เราจำเป็นต้องดำเนินการดับเบิลคลิกบนองค์ประกอบเว็บ
วิธีการ Double คลิกเข้า Selenium
สถานการณ์ทดสอบ
- เปิดตัว URL: https://demo.guru99.com/test/simple_context_menu.html
- Double คลิกที่ปุ่มที่มีข้อความ 'Double- คลิกฉันเพื่อดูการแจ้งเตือน'
- คลิกปุ่มตกลงบนการแจ้งเตือนที่แสดง
รหัส:
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 DobuleClickDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","X://chromedriver.exe");
driver= new ChromeDriver();
//Launch the Application Under Test (AUT)
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
driver.get("https://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
//Double click the button to launch an alertbox
Actions action = new Actions(driver);
WebElement link =driver.findElement(By.xpath("//button[text()='Double-Click Me To See Alert']"));
action.doubleClick(link).perform();
//Switch to the alert box and click on OK button
Alert alert = driver.switchTo().alert();
System.out.println("Alert Text\n" +alert.getText());
alert.accept();
//Closing the driver instance
//driver.quit();
}
}
ผลลัพธ์:
ปุ่มที่มีป้ายกำกับว่า “Double- คลิก Me to See Alert” แล้วป๊อปอัปจะปรากฏขึ้น
In Eclipseคุณจะเห็นผลลัพธ์ในคอนโซล
สรุป
- ชั้นเรียนการกระทำใน Selenium ส่วนใหญ่ใช้เพื่อดำเนินการแป้นพิมพ์และเมาส์ที่ซับซ้อน ดังนั้นคลาส Actions จึงเป็นที่นิยมมากกว่า Javascript สำหรับการดำเนินการต่างๆ เช่น คลิกขวาและ Double คลิกเข้า Selenium.
- การคลิกขวามักใช้เมื่อคลิกขวาที่องค์ประกอบเพื่อเปิดเมนูใหม่ การคลิกขวาใน Selenium ไดรเวอร์เว็บสามารถทำได้โดยใช้คำสั่ง Context Click ที่กำหนดไว้ล่วงหน้าดังที่กล่าวไว้ด้านล่าง
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action.contextClick(link).perform(); - Double การดำเนินการคลิกจะใช้เมื่อสถานะขององค์ประกอบเว็บเปลี่ยนแปลงหลังจากการดับเบิลคลิก Double คลิกการดำเนินการใน Selenium ไดรเวอร์เว็บสามารถทำได้โดยใช้คำสั่งที่กำหนดไว้ล่วงหน้า Double คลิกตามที่ระบุไว้ด้านล่างนี้
Actions action = new Actions(driver); WebElement link = driver.findElement(By.ID ("Element ID")); action. doubleClick (link).perform();



