如何点击div,span中的元素

时间:2014-12-08 21:54:55

标签: java selenium selenium-webdriver

下面是我的脚本和HTML。我无法点击下拉菜单,然后选择"十二"从列表中。我试过用xpath,名字,但似乎没什么用。任何帮助是极大的赞赏。谢谢!

我的剧本:

WebElement temp = driver.finaElement(By.cssSelector("#s2id_campaignStatus > a.select2-choice > span"));
temp.click();

HTML:

<table>
    <tbody>
        <tr>
            <td>
                <div>
                    <div class="select2-container select2-container-active" id="s2id_campaignStatus">
                        <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1">                                                  
                        <span>twelve</span>
                            <abbr class="select2-search-choice-close" style="display:none;"></abbr>
                            <div>
                                <b></b></div>
                                </a><input class="select2-focusser select2-offscreen" type="text"></div>
                                <select name="campaignStatus" id="campaignStatus" class="select2-offscreen" tabindex="-1">


                                <option value="W" selected="selected">ABC</option>

                                            <option value="L">one</option>
                                            <option value="P">two</option>
                                            <option value="O">three</option>
                                            <option value="C">four</option>
                                            <option value="R">five</option>
                                            <option value="J">six</option>
                                            <option value="X">seven</option>
                                            <option value="S">eight</option>
                                            <option value="A">nine</option>
                                            <option value="D">ten</option>
                                            <option value="T">twelve</option>
                                </select>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:12px;">
                            <a href="#" onclick="showTemplate()">
                                What is a Template?
                            </a>
                        </td>
                    </tr>
                </tbody></table>

我的测试代码@Saifur:

new Select(driver.findElement(By.id("campaignStatus"))).selectByValue("T");

        By elementId = By.id("campaignStatus");
        WebDriverWait wait = new WebDriverWait(driver, 8);
        wait.until(ExpectedConditions.presenceOfElementLocated(elementId));
        new Select(driver.findElement(elementId)).selectByValue("T");

2 个答案:

答案 0 :(得分:0)

new Select(driver.findElement(By.id("campaignStatus"))).selectByVisibleText("twelve");

使用Select课程,您还有多个选项。见this

new Select(driver.findElement(By.id("campaignStatus"))).selectByValue("T");

编辑添加等待元素处于就绪状态

By elementId = By.id("campaignStatus");
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(elementId );
new Select(driver.findElement(elementId)).selectByValue("T"); 

答案 1 :(得分:0)

请尝试下面的代码,它应该适合你

在下拉列表中选择“ 12 ”之前和之后,我已经给出了2秒的手动等待。

明确等待 8秒的时间等待,直到选择元素获得点击。

WebDriverWait wait = new WebDriverWait(driver, 8);
WebElement Manages = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='campaignStatus']")));
Select dropDownelement = new Select(Manages);
Thread.sleep(2000);
dropDownelement.selectByVisibleText("twelve");
Thread.sleep(2000);