Selenium下载java中的问题

时间:2012-11-21 08:44:20

标签: java selenium webdriver selenium-webdriver

我有一个网站,我正在尝试自动化我能够登录并单击其中一个菜单,但我遇到问题,当我点击第三个孩子

说我有一个主菜单显示子菜单,点击子菜单它显示第三个菜单,其中有10个链接以自上而下的顺序显示我点击了第9个孩子,我无法做到。

以下是代码:

WebElement myElement1 = driver.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/span/span/span"));
Actions builder = new Actions(driver);
builder.moveToElement(myElement1).build().perform();
myElement1.click();

WebElement myElement2 = driver.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/span/span/span"));
Actions builder1 = new Actions(driver);
builder1.moveToElement(myElement2).build().perform();
myElement2.click();

WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement ele = driver.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/ul/li[9]/a/span/span"));
System.out.println(ele.getText());

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/ul/li[9]/a/span/span")));
System.out.println(" about to get enter time sheet" + element.getText());
System.out.println(isElementPresent(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/ul/li[9]/a/span/span")));
driver.manage().timeouts().implicitlyWait(2500, TimeUnit.SECONDS);
org.openqa.selenium.Point coordinates = driver.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/ul/li[9]/a/span/span")).getLocation();
System.out.println(coordinates.getX() + " " + coordinates.getY());
WebElement myElement3 = driver.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/table/tbody/tr/td[2]/div/div/ul/li/ul/li[2]/ul/li/ul/li[9]/a/span/span"));
Actions builder3 = new Actions(driver);
builder3.moveToElement(myElement3).build().perform();
myElement3.click();

最后一次下拉是失败的地方。

1 个答案:

答案 0 :(得分:0)

为selenium 2.0提交了类似的错误并已关闭。请参考最新稳定的硒。

让我们参考以下菜单下拉菜单中的2个级别。

Menu
  Item 1
      SubItem1
      SubItem2
  Item 2

我会建议这个问题的两种方法

从上面的代码

方法1: 我看到为Level1菜单项和Level2菜单项定义了点击事件。而是直接引用第2级项目而不单击第1级项目,如下所示:

Menu.click();
Actions Build = new Actions(driver);
Build.moveToElement(Menu).moveToElement(Item1).moveToElement(SubItem2).click().build().perform();

方法2 JavaScript方式,虽然点击页面上的菜单项是安全的工作。

js.ExecuteScript("return $(\"a:contains('Menu')\").mouseover();");
js.ExecuteScript("return $(\"a:contains('Item1')\").next(':eq(1)').mouseover();");
driver.FindElement(By.LinkText("SubItem2")).Click();

以下是方法1的实时示例...(C#代码段和亚马逊网站)

        IWebDriver driver = new InternetExplorerDriver();
        driver.Navigate().GoToUrl("http://www.amazon.com/gp/goldbox/ref=cs_top_nav_gb27");
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

        IWebElement Menu = driver.FindElement(By.Id("nav-bar-inner")).FindElement(By.Id("nav-shop-all-button"));
        Menu.Click();

        IWebElement Item1 = driver.FindElement(By.Id("nav_cat_4"));
        Actions Build = new Actions(driver);
        Build.MoveToElement(Menu).MoveToElement(Item1).Click().Build().Perform();

        IWebElement SubItem = driver.FindElement(By.Id("nav_subcats_4")).FindElement(By.LinkText("Games"));
        Build.MoveToElement(Menu).MoveToElement(Item1).MoveToElement(SubItem).Click().Build().Perform();

以下是亚马逊网站的示例,首先点击顶级菜单,然后点击Item1(级别1)然后点击它通过Level1通过TopMenu点击Item2

我希望这有助于......一切顺利:)