java robot 右键,在Selenium Webdriver – Java中的右键菜单中select一个选项

我们将采取WebDriver操作类的帮助,并执行右键单击。 以下是语法:

Actions action = new Actions(driver).contextClick(element); action.build().perform();

以下是我们在示例中所遵循的步骤:

识别元素

等待元素的存在

现在执行上下文点击

之后,我们需要select所需的链接。

包com.pack.rightclick;

import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class RightClickExample { WebDriver driver; String URL = "http://medialize.github.io/jQuery-contextMenu/demo.html"; @BeforeClass public void Setup() { driver = new FirefoxDriver(); driver.manage().window().maximize(); } @Test public void rightClickTest() { driver.navigate().to(URL); By locator = By.cssSelector(".context-menu-one.box"); WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.presenceOfElementLocated(locator)); WebElement element=driver.findElement(locator); rightClick(element); WebElement elementEdit =driver.findElement(By.cssSelector(".context-menu-item.icon.icon-edit>span")); elementEdit.click(); Alert alert=driver.switchTo().alert(); String textEdit = alert.getText(); Assert.assertEquals(textEdit, "clicked: edit", "Failed to click on Edit link"); } public void rightClick(WebElement element) { try { Actions action = new Actions(driver).contextClick(element); action.build().perform(); System.out.println("Sucessfully Right clicked on the element"); } catch (StaleElementReferenceException e) { System.out.println("Element is not attached to the page document " + e.getStackTrace()); } catch (NoSuchElementException e) { System.out.println("Element " + element + " was not found in DOM " + e.getStackTrace()); } catch (Exception e) { System.out.println("Element " + element + " was not clickable " + e.getStackTrace()); } } @AfterClass public void tearDown() { driver.quit(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值