【问题标题】:Drop down click is not working in selenium java webdriver?下拉单击在 selenium java webdriver 中不起作用?
【发布时间】:2013-08-26 14:05:59
【问题描述】:

谁能告诉我我的代码有什么问题。

WebElement dropdown = driver.findElement(By.xpath("//*[@id='idCallType_100']"));
        List<WebElement> Options = dropdown.findElements(By.tagName("option"));       
        System.out.println(Options.size());       
        for(int i=0;i<Options.size();i++)
        {
            WebElement o = Options.get(i);
            System.out.println(Options.get(i).getText()+ " ---- " + Options.get(i).getAttribute("value"));
            driver.findElement(By.xpath("//*[@id='idInsertTaskButton']/a")).click();         **// I couldnt able to click on this line**
            Options.get(i).click();
            System.out.println("coming for email time");
            driver.findElement(By.xpath("//*[@id='idInsertTask_100']/div/div[1]/input")).click();   

        }

我正在尝试单击下拉菜单。对于第一次单击它的工作正常。下次不从该行执行时形成
Options.get(i).click(); 我不知道这里出了什么问题。

Thanks!

【问题讨论】:

  • 怎么不行?它会出错吗?什么?试过不同的浏览器?什么浏览器?什么版本的 Selenium?
  • 我在浏览器中使用 selenium 2.35,更新版本的 Firefox 和 chrome 并且它第一次工作,从第二次开始就不能工作。而且我错误地说该元素在移动时不可点击。谢谢!
  • 您想从下拉菜单中选择一个项目吗?尝试使用“选择”而不是您正在使用的代码。

标签: java testing xpath junit selenium-webdriver


【解决方案1】:

只需将您的 WebElement 包装到 Select Object 中,如下所示

Select dropdown = new Select(driver.findElement(By.id("identifier")));

完成后,您可以通过 3 种方式选择所需的值。考虑一个这样的 HTML 文件

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

现在来识别下拉菜单

Select dropdown = new Select(driver.findElement(By.id("designation")));

要选择它的选项说“程序员”,你可以这样做

dropdown.selectByVisibleText("Programmer ");

dropdown.selectByIndex(1);

dropdown.selectByValue("prog");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多