使用Selenium自动完成

时间:2016-04-08 07:47:26

标签: selenium

我有一些(实际上,很多)麻烦自动从Selenium的自动完成选项中进行选择。目前,我能够自动输入文本,但是,我无法从弹出的显示下拉建议列表中选择任何内容。我试着在这里搜索我的问题的一些答案,但没有任何效果。以下是与我尝试选择的建议一起显示的元素:

<div class="cs-autocomplete-popup">
    <div class="inner">

        <div class="cs-autocomplete-Matches csc-autocomplete-Matches">
            <ul>
                <li class="cs-autocomplete-matchItem csc-autocomplete-matchItem">
                    <span class="csc-autocomplete-matchItem-content cs-autocomplete-matchItem-content" id="matchItem::matchItemContent">john doe</span>
                </li>
            </ul>
        </div>
        <div class="csc-autocomplete-addToPanel cs-autocomplete-addToPanel">
            <hr>
            <div class="content csc-autocomplete-addTermTo cs-autocomplete-addTermTo">Add "John Doe" to:</div>
            <ul>
                <li class="cs-autocomplete-authorityItem csc-autocomplete-authorityItem" id="authorityItem:">Local Persons</li>
            </ul>
        </div>
    </div>
    <div class="cs-autocomplete-popup-miniView csc-autocomplete-popup-miniView" style="top: 2px; left: 149px; display: none;"><div class="cs-miniView">
    <a href="../html/person.html?csid=urn:cspace:name(johndoe1456528631946)&amp;vocab=person" class="csc-autocomplete-popup-miniView-displayName cs-autocomplete-popup-miniView-displayName">john doe</a>
    <div>
        <span class="csc-autocomplete-popup-miniView-field1Label cs-autocomplete-popup-miniView-field1Label">b.</span>
        <span class="csc-autocomplete-popup-miniView-field1 cs-autocomplete-popup-miniView-field1" id="field1"></span>
    </div>
    <div>
        <span class="csc-autocomplete-popup-miniView-field2Label cs-autocomplete-popup-miniView-field2Label">d.</span>
        <span class="csc-autocomplete-popup-miniView-field2 cs-autocomplete-popup-miniView-field2" id="field2"></span>
    </div>
    <div>

        <span class="csc-autocomplete-popup-miniView-field3 cs-autocomplete-popup-miniView-field3" id="field3"></span>
    </div>
    <div>


    </div>
</div></div>
</div>

由此,我试图选择&#34; john doe&#34;。有谁知道这样做的有效/完整方式?我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

    driver.findElement(By.xpath("get the exact field address of autocomplete textbox"); 

    Thread.sleep(5000);
    //for xpath: need to take the common xpath from the list of the elements.

    List<WebElement> link=driver.findElements(By.xpath("//span[contains(@class, 'cs-autocomplete-matchItem-content') and .='john doe']");

    for(int i=0; i<=link.size(); i++)
     {

        if(link.get(i).getText().equalsIgnoreCase("john doe");
         {
        link.get(i).click();
         }
    }