用于创建新的acc Gmail表单的测试用例 - 无法选择"月"落下

时间:2015-06-29 21:19:38

标签: selenium

我是selenium webdriver的新手, 我正在编写测试用例 - "在gmail中创建新帐户"无法选择月份..

以下是我使用的代码:

    driver.findElement(By.id("link-signup")).click();
    WebElement element =driver.findElement(By.id("BirthMonth"));
    Select mSelection = new Select(element);
    mSelection.selectByVisibleText("July");

我收到错误:: 意外的标记名例外:元素应该已被选中但是是跨度

- 我在html代码中找不到任何选择标签..如何解决此问题

html code ::

<label id="month-label" class="month">
<span id="BirthMonth" class=" " aria-invalid="false">
<div class="goog-inline-block goog-flat-menu-button jfk-select goog-flat-menu-button-hover goog-flat-menu-button-focused" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" title="" aria-activedescendant=":0">
<div id=":0" class="goog-inline-block goog-flat-menu-button-caption" role="option" aria-setsize="12" aria-posinset="0">Month</div>
<div class="goog-inline-block goog-flat-menu-button-dropdown" aria-hidden="true"/>
</div>
<div class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: -107.133px; display: none;" role="listbox" aria-haspopup="true">
<div id=":1" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":2" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":3" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":4" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":5" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":6" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":7" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":8" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":9" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":a" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":b" class="goog-menuitem" role="option" style="-moz-user-select: none;">
<div id=":c" class="goog-menuitem" role="option" style="-moz-user-select: none;">
</div>
<input id="HiddenBirthMonth" type="hidden" name="BirthMonth"/>
</span>
</label>

- 感谢

1 个答案:

答案 0 :(得分:0)

如果您没有在那里找到任何选择标记,并且您想要选择一个月:您可以单击月份元素并选择月份。

//Click on the month listbox
driver.findElement(By.xpath(".//*[@id='BirthMonth']/div")).click();

//Select the february month
driver.findElement(By.xpath(".//*[@id=':2']/div")).click();

每个月都有不同的ID,因此您可以选择所需的月份。

您还可以在列表中查看所有月份并选择您需要的月份

   List<WebElement> months = driver.findElements(By.xpath("//*[@class='goog-menuitem']"));
        for (int i=0; i<months.size(); i++){
            if (months.get(i).getText().trim().equals("July"))
                months.get(i).click();
        }

以上代码将从列表中选择7月