Mon*_*dav 24 java selenium-webdriver
我正在使用Selenium webdriver.我无法从右键单击打开的选项中选择(比如说第二个)选项.
在我当前的代码中,我可以右键单击webElement但无法从右键单击后打开的列表中选择一个选项,因为它会自动消失.
Actions action= new Actions(driver);
action.contextClick(productLink).build().perform();
Run Code Online (Sandbox Code Playgroud)
因此,使用此代码,我可以右键单击,但右键菜单会自动消失.我想从右键菜单中选择说第二个选项.
请帮忙!!!
小智 31
要从上下文菜单中选择项目,您只需使用按键向下事件移动鼠标位置,如下所示: -
Actions action= new Actions(driver);
action.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Run Code Online (Sandbox Code Playgroud)
希望这对你有用.祝你有美好的一天 :)
小智 7
这是更好的方法,它的成功:
Actions oAction = new Actions(driver);
oAction.moveToElement(Webelement);
oAction.contextClick(Webelement).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();
Run Code Online (Sandbox Code Playgroud)
*使用Robot类可以执行此操作,请尝试以下代码:
Actions action = new Actions(driver);
action.contextClick(WebElement).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Run Code Online (Sandbox Code Playgroud)
小智 5
我们将借助 WebDriver 操作类并执行右键单击。以下是语法:
Actions action = new Actions(driver).contextClick(element);
action.build().perform();
Run Code Online (Sandbox Code Playgroud)
以下是我们在示例中遵循的步骤:
包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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119010 次 |
| 最近记录: |