繁体   English   中英

如何单击div中的元素,跨度

[英]How to click on the element in div, span

以下是我的脚本和html。 我无法单击下拉菜单,然后从列表中选择“十二”。 我已经尝试使用xpath,name,但是似乎没有任何效果。 任何帮助是极大的赞赏。 谢谢!

我的脚本:

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");
new Select(driver.findElement(By.id("campaignStatus"))).selectByVisibleText("twelve");

您还可以使用Select类有多个选项。 看到这个

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"); 

请尝试以下代码,它应该适合您

从下拉列表中选择“ 十二 ”之前和之后,我已经进行了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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM