二十一、模拟鼠标右键事件
被测试网页的网址:
http://www.sogou.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 import org.openqa.selenium.interactions.Actions; 10 import org.testng.annotations.AfterMethod; 11 12 public class ChormeOpen { 13 WebDriver driver; 14 String url = "http://www.sogou.com"; 15 16 @Test 17 public void opentest() { 18 driver.get(url); 19 try { 20 Thread.sleep(3000); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 Actions action = new Actions(driver); 26 action.contextClick(driver.findElement(By.id("query"))).perform(); 27 try { 28 Thread.sleep(3000); 29 } catch (InterruptedException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 @BeforeMethod 36 public void beforeMethod() { 37 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 38 driver = new ChromeDriver(); 39 } 40 41 @AfterMethod 42 public void afterMethod() { 43 driver.quit(); 44 } 45 46 }