从右键单击菜单中,它没有单击特定选项Selenium

时间:2016-04-27 11:00:30

标签: java selenium

在一个项目中,我想右键单击一个选项,然后从那里我想选择"在新窗口中打开链接"。我写了以下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*/



    }

}

1 个答案:

答案 0 :(得分:0)

HI而不是单击使用ENTER它将起作用

oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

为什么点击不起作用会导致在给定的webElement中间单击点击而新的Tab选项不是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();
}
}

希望这能解决您的疑问