2012-07-11 179 views
19

我使用的是Selenium webdriver。我无法从右键单击打开的选项中选择(如第二个)选项。从Selenium Webdriver的右键菜单中选择一个选项 - Java

在我当前的代码中,我可以右键单击webElement,但无法从右键单击后打开的列表中选择一个选项,因为它会自动消失。

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

因此,使用此代码我可以右键单击,但右键菜单自动消失。我想从右键菜单中选择说第二个选项。

请帮忙!!!

回答

27

从上下文菜单中选择的项目,你必须只动鼠标位置使用像这样的按键事件: -

Actions action= new Actions(driver); 
action.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform(); 

希望这会适用于你。 有一个美好的一天:)

+0

@ Neeraj-这是正常工作....非常感谢! – 2012-07-12 10:53:42

1

您可能需要将鼠标移动到任何特定的位置背景点击()这样的后 -

Actions action = new Actions(driver); 
actions.contextClick(link).moveByOffset(x,y).click().build().perform(); 

要了解如何moveByOffset(X,Y)的作品看here;

我希望这可以工作。您将必须计算xy的偏移值;

最好的方法是在右击之后找到每个选项按钮的大小,然后点击第二个选项。

X = 宽度选项按钮的/2

Y = 2 *(大小每个选项按钮的)

+0

@ Hari Reddy - 感谢您的回答,目前的解决方案问题是,我不能o获取偏移值,因为“咔嗒菜单选项”无法在萤火虫中检查。所以无法在Firebug>布局中获得偏移量。 – 2012-07-11 11:14:54

+0

是的我明白你的问题。即使我不确定我们如何找到右键单击后所获得的选项的宽度。你可以尝试一些命中和追踪的方式,尝试增加5或10。让我知道会发生什么? – 2012-07-11 12:39:13

0

这里是右键单击一个webelement的代码。

Actions actions = new Actions(driver);  
Action action=actions.contextClick(WebElement).build(); //pass WebElement as an argument 
      action.perform(); 
4

这是更好的方法和它的成功:

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(); 
+0

可能适用于自定义菜单,但不适用于Chrome浏览器菜单 – iirekm 2017-03-31 14:07:43

3

我们将采取行动的webdriver类的帮助,并执行右键单击。下面是语法:

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

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

  1. 标识元素
  2. 等待元素的存在
  3. 现在执行上下文点击
  4. 之后,我们需要选择所需的链接。

package com.pack。右键点击;

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(); 
    } 


} 
0

这就是我如何点击Right click window中的第四个元素。

Actions myAction = new Actions(driver); 
myAction.contextClick(driver.findElement(By.xpath("//ul/li[1]/a/img"))).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ENTER).build().perform(); 

希望这有助于

1

*使用机器人类,你可以做到这一点,试试下面的代码:

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);* 
0

点击右键可以使用Java脚本执行器以及可实现(在情况下采取行动类不支持):

JavascriptExecutor js = (JavascriptExecutor) driver; 

String javaScript = "var evt = document.createEvent('MouseEvents');" 
       + "var RIGHT_CLICK_BUTTON_CODE = 2;" 
       + "evt.initMouseEvent('contextmenu', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, RIGHT_CLICK_BUTTON_CODE, null);" 
       + "arguments[0].dispatchEvent(evt)"; 

js.executeScript(javaScript, element); 
相关问题