29

我正在使用 Selenium 网络驱动程序。我无法从右键单击打开的选项中选择(比如说第二个)选项。

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

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

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

请帮忙!!!

4

12 回答 12

34

要从上下文菜单中选择项目,您只需使用 Key down 事件移动鼠标位置,如下所示:-

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

希望这对你有用。祝你有美好的一天 :)

于 2012-07-12T10:46:38.843 回答
9

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

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

[更新]

注意:您的浏览器应始终处于焦点位置,即在执行机器人动作时在前台运行,否则前台的任何其他应用程序都会收到这些动作。

于 2017-01-04T09:11:47.990 回答
8

这是更好的方法并且它是成功的:

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();
于 2014-03-13T11:06:11.247 回答
4

我们将借助 WebDriver 动作类并执行右键单击。以下是语法:

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

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

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

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


}
于 2015-10-08T07:15:50.660 回答
2

不要尝试右键单击鼠标,而是使用键盘快捷键:

双击元素 -> 按住 shift 并按 F10。

Actions action = new Actions(driver);

//Hold left shift and press F10
action.MoveToElement(element).DoubleClick().KeyDown(Keys.LeftShift).SendKeys(Keys.F10).KeyUp(Keys.LeftShift).Build().Perform();
于 2019-03-21T14:29:58.610 回答
1

在上下文 click() 之后,您可能必须将鼠标移动到任何特定位置,如下所示 -

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

要了解 moveByOffset(x,y) 的工作原理,请看这里

我希望这行得通。您必须计算xy的偏移值;

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

x =选项按钮的宽度/2

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

于 2012-07-11T09:56:42.230 回答
0

这是右键单击 web 元素的代码。

Actions actions = new Actions(driver);    
Action action=actions.contextClick(WebElement).build(); //pass WebElement as an argument
            action.perform();
于 2014-02-17T14:43:36.147 回答
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();

希望这可以帮助

于 2016-07-26T05:00:04.500 回答
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);
于 2017-10-26T04:33:51.857 回答
0

更好更简单的方法。

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.cssSelector("a[href^='https://www.amazon.in/ap/signin']"))).contextClick().build().perform();

您可以在 的位置使用任何选择器cssSelector

于 2018-09-26T07:37:18.810 回答
0

使用python webdriver右键操作

from selenium import webdriver

from selenium.webdriver import ActionChains

import time

driver = webdriver.Chrome()

driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html")
button=driver.find_element_by_xpath("//body[@class='wy-body-for-nav']")

action=ActionChains(driver)
action.context_click(button).perform()
于 2018-11-29T06:27:19.100 回答
0
WebElement xx = driver.findElement(By.linkText("your element"));
                Actions action = new Actions(driver);
                System.out.println("To open new tab");
                  action.contextClick(xx).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.VK_DOWN);
                    robot.keyPress(KeyEvent.VK_ENTER);
于 2019-09-13T09:11:46.047 回答