Java 在右键单击菜单中,它没有单击特定选项

Java 在右键单击菜单中,它没有单击特定选项,java,selenium,Java,Selenium,在一个项目中,我想右击一个选项,然后从中选择“在新窗口中打开链接”。我已经编写了以下selenium java代码。在这段代码中,它在“在新窗口中打开链接”上爬行,但在此之后,它不会单击该选项在新窗口中打开我所需的链接。如果需要,可以直接复制并粘贴我的代码以可视化执行流。请帮助我了解我犯的错误在哪里…我的意图是在新窗口中打开一个链接 package pack_001; import java.util.Set; import java.util.concurrent.TimeUnit; im

在一个项目中,我想右击一个选项,然后从中选择“在新窗口中打开链接”。我已经编写了以下selenium java代码。在这段代码中,它在“在新窗口中打开链接”上爬行,但在此之后,它不会单击该选项在新窗口中打开我所需的链接。如果需要,可以直接复制并粘贴我的代码以可视化执行流。请帮助我了解我犯的错误在哪里…我的意图是在新窗口中打开一个链接

package pack_001;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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.JavascriptExecutor;
import org.openqa.selenium.Keys;

public class mail {
    public static WebDriver driver=new FirefoxDriver();
    public static void main(String args[])
    {
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.get("http://www.tutorialspoint.com/java/");
            driver.manage().window().maximize();
            WebElement env=driver.findElement(By.linkText("Java - Environment Setup"));
            System.out.println("Env point out");
            Actions oAction = new Actions(driver);

            oAction.moveToElement(env);
            oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).click().build().perform();  /* this should click on the option*/



    }

}

HI,而不是单击“使用输入”,它将工作

oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
还有为什么单击不起作用,因为单击位于给定webElement的中间,而新选项卡选项不是webElement

这肯定会在new windwo中打开选项卡

更新

public class DropDownSelection {
    public static WebDriver driver=new FirefoxDriver();
    public static void main(String[] args) {
           driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

       // i have used a sample url exactly as similar to your problem
       driver.get("http://desirulez.biz/category/wrestlingnetwork/wwe/wwe-raw/");
       driver.manage().window().maximize();

       // way One
       Actions oAction = new Actions(driver);

       // now simply hover over the WWE - Menu in your case
       WebElement Menu = driver.findElement(By.xpath("//*[@id='menu-item-8']/a"));
       oAction.moveToElement(Menu).build().perform();
       // hovering will open its sub-menu - Configure in your case
       // now identify the sub-menu that you want to click/hover
       WebElement Configure = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/a"));
       oAction.moveToElement(Configure).build().perform();
       // now hovering over it will open its sub menu
       // in your case Manage
       // now identify the sub-menu that you want to click/hover
       WebElement Manage = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/ul/li/a"));
       oAction.moveToElement(Manage).click().build().perform();

       // way Two
       // note if you want to chain above you can even do that like below
       oAction.moveToElement(Menu).moveToElement(Configure).moveToElement(Manage).click().build().perform();
}
}

希望这能解决您的查询问题,而不是单击“使用输入”按钮。它会工作的

oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
还有为什么单击不起作用,因为单击位于给定webElement的中间,而新选项卡选项不是webElement

这肯定会在new windwo中打开选项卡

更新

public class DropDownSelection {
    public static WebDriver driver=new FirefoxDriver();
    public static void main(String[] args) {
           driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

       // i have used a sample url exactly as similar to your problem
       driver.get("http://desirulez.biz/category/wrestlingnetwork/wwe/wwe-raw/");
       driver.manage().window().maximize();

       // way One
       Actions oAction = new Actions(driver);

       // now simply hover over the WWE - Menu in your case
       WebElement Menu = driver.findElement(By.xpath("//*[@id='menu-item-8']/a"));
       oAction.moveToElement(Menu).build().perform();
       // hovering will open its sub-menu - Configure in your case
       // now identify the sub-menu that you want to click/hover
       WebElement Configure = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/a"));
       oAction.moveToElement(Configure).build().perform();
       // now hovering over it will open its sub menu
       // in your case Manage
       // now identify the sub-menu that you want to click/hover
       WebElement Manage = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/ul/li/a"));
       oAction.moveToElement(Manage).click().build().perform();

       // way Two
       // note if you want to chain above you can even do that like below
       oAction.moveToElement(Menu).moveToElement(Configure).moveToElement(Manage).click().build().perform();
}
}


希望这能解决您的查询

为了获得相同的效果,您可以创建一个新的
WebDriver
并导航到该链接。例如:
driver2.get(env.getAttribute(“href”)WebDrive
,例如:
WebDriver driver2=new FirefoxDriver()
,要在此新窗口中打开
元素
链接,可以执行以下操作
drive2.get(element.getAttribute(“href”)。如果
元素
是HTML
元素,这将起作用。另外,Firefox还有一个快捷方式,可以在新窗口中打开链接
SHIFT+左键单击
。谢谢你的解释嘿,伙计,你救了我。。我根据需要和环境修改了您的解决方案。它帮助我解决了这个问题。再次感谢你,伙计。为了获得同样的效果,你可以创建一个新的
WebDriver
并导航到该链接。例如:
driver2.get(env.getAttribute(“href”)WebDrive
,例如:
WebDriver driver2=new FirefoxDriver()
,要在此新窗口中打开
元素
链接,可以执行以下操作
drive2.get(element.getAttribute(“href”)。如果
元素
是HTML
元素,这将起作用。另外,Firefox还有一个快捷方式,可以在新窗口中打开链接
SHIFT+左键单击
。谢谢你的解释嘿,伙计,你救了我。。我根据需要和环境修改了您的解决方案。它帮助我解决了这个问题。再次感谢你,伙计。让我再向你解释一下我的目标。您的解决方案在大多数网站上都运行良好,我还发现这是一种方便的方式。但是我试图在新窗口中打开链接的环境并不稳定。有时它是有效的,有时它是失败的。流程是这样的-它将单击“菜单”选项->dro下拉列表->选择“配置”选项->另一个子菜单->现在我需要在新窗口中打开“管理”。因此,在“管理”选项之前没有问题。但是上面的解决方案是有时点击“管理”成功请纠正我如果我错了你试图打开一个管理菜单,它是配置的子菜单,配置是菜单的子菜单如果是,那么在这种情况下,为什么你要使用上下文点击,它可以直接实现,而不需要,上面的解决方案也是鼠标右键操作PLZ确认,这样我就可以帮助您解决问题的工作代码,无论您理解什么都是绝对正确的。你的代码也很好。但我的错误是,我无法解释使用contextClick()打开它的正确意图。实际上是哪个环境。我正在编写的代码应该与Firefox和Chrome兼容。现在,只要我们在Firefox中单击“管理”选项,就会打开一个新的窗口来管理选项。因此,我可以直接切换到()并继续我的自动化。但当同样的操作在Chrome上进行时,它会在同一窗口下的新选项卡上打开“管理”。因此switchTo()无法识别新选项卡,…(续),因为switchTo()用于在窗口之间而不是在选项卡之间更改焦点。这就是为什么,对于chrome,我试图在一个新窗口上显式打开“管理”选项。这样我就可以生成一个要切换的窗口id。让我再解释一下我的目标。您的解决方案在大多数网站上都运行良好,我还发现这是一种方便的方式。但是我试图在新窗口中打开链接的环境并不稳定。有时它是有效的,有时它是失败的。流程是这样的-它将单击“菜单”选项->dro下拉列表->选择“配置”选项->另一个子菜单->现在我需要在新窗口中打开“管理”。因此,在“管理”选项之前没有问题。但是上面的解决方案是有时点击“管理”成功请纠正我如果我错了你试图打开一个管理菜单,它是配置的子菜单,配置是菜单的子菜单如果是,那么在这种情况下,为什么你要使用上下文点击,它可以直接实现,而不需要,上面的解决方案也是鼠标右键操作PLZ确认,这样我就可以帮助您解决问题的工作代码,无论您理解什么都是绝对正确的。你的代码也很好。但我的错误是,我无法解释使用contextClick()打开它的正确意图。实际上是哪个环境。我正在写的代码