如何在selenium testng中编写rightclick方法

问题描述 投票:0回答:2

怎么做我右键点击webelement selenium testng?我给了你doubleclick的例子,同样我需要右键单击方法。请给我最好的一个。

public static void doubleclickOn(String objLocator1){                   

            try
            {
            findWebElement(objLocator1);


            Actions actions = new Actions(driver);    
        org.openqa.selenium.interactions.Action action = actions.doubleClick(webElement).build();
            action.perform();
            APP_LOGS.debug("double Clicked on "+locatorDescription);
            //System.out.println(locator);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                APP_LOGS.debug("FAIL : The locator "+locator+" of description "+locatorDescription+": does not exists in webpage:");
                Reporting.fail("FAIL : The locator "+locator+" of description "+locatorDescription+": does not exists in webpage:");

            }

}

提前致谢

selenium testng
2个回答
0
投票

试试这个:-

假设“objlocator1”由右键单击的webelement的xpath组成。

public static void rightClickOn(String objLocator1){                   

            try
            {
            findWebElement(objLocator1);


            Actions actions = new Actions(driver);    
            actions.contextClick(driver.findElement(By.xpath(objLocator1)));
            actions.perform();

            APP_LOGS.debug("Context Clicked on "+locatorDescription);
            //System.out.println(locator);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                APP_LOGS.debug("FAIL : The locator "+locator+" of description "+locatorDescription+": does not exists in webpage:");
                Reporting.fail("FAIL : The locator "+locator+" of description "+locatorDescription+": does not exists in webpage:");

            }

0
投票

可以使用Actions类在Selenium Web驱动程序中执行右键单击操作。

也称为Context Click。

1)下面是使用Actions类演示右键单击操作的代码。

Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.id("ID"));
actions.contextClick(elementLocator).perform();

2)从上下文菜单中选择项目

Actions action= new Actions(driver);  
WebElement elementLocator = driver.findElement(By.id("ID"));    
action.contextClick(elementLocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform(); 
//adjust keys.ARROW_DOWN accordingly
© www.soinside.com 2019 - 2024. All rights reserved.