如何在selenium testng中编写rightclick方法

如何在selenium testng中编写rightclick方法,selenium,testng,Selenium,Testng,如何右键单击Web元素selenium testng? 我给你们举了双击的例子,同样我也需要右键点击的方法。请给我最好的一个 public static void doubleclickOn(String objLocator1){ try { findWebElement(objLocator1); Actions actions = new Action

如何右键单击Web元素selenium testng? 我给你们举了双击的例子,同样我也需要右键点击的方法。请给我最好的一个

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

            }
}

提前感谢

试试这个:-

假设“objlocator1”包含要右键单击的Web元素的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:");

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

Selenium web驱动程序中的右键单击操作可以使用操作类完成

也称为上下文单击

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

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

            }
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

请尝试自己制定代码,然后粘贴,以防遇到路障,而不是要求人们完全完成您的工作。让你的目标达成一半,这样人们就会帮助你实现另一个目标。:)我不是在不知道的情况下问别人,只是澄清一下,我写了双击代码subh,也用双击的相同方式尝试了右键单击方法,它的工作也很好。但是它的工作速度很慢,所以我需要比我做的更好。我没有要求人们充分完成我的工作@请不要误会我。如果您提交了用于上下文单击的代码,然后询问了与优化或其他备选方案相关的问题,那么情况就不同了。在这里,人们倾向于对这样的问题表示否定。因此,我只是要求你们采取预防措施……)我的错。我现在已经编辑了上下文单击的代码。以前是双击的。以前对rightclick方法执行过相同的代码。实际上,当我运行我的程序时,右键单击对象定位器是很慢的。所以我需要右键单击的其他替代方法据我所知,Selenium webdriver中只有两种右键单击方法,即contextClick()和contextClick(WebElement OneElement)。而且,由于您希望右键单击某个元素,后面的元素就足够了。至于速度慢,则取决于浏览器**(仅供参考,IE的速度相对较慢,其次是Firefox和Chrome)**。恐怕就是这样。@Vids:就硒而言,除了这个,没有其他最佳方法。但是,您可以调整xpath(如果您正在使用它),使其成为相对的。我还有一个问题,当我们在web应用程序中使用自动化时,我的程序一次通过,同一个程序一次失败。如果发生了?怎么办?