从列表框中选择元素

时间:2017-01-24 08:25:38

标签: c# html selenium xpath automation

如何从此列表中选择元素?

ClickElementByXPath("//li[contains(text(), 'Legnica')]");

ClickElementByXPath("//li[contains(., 'Legnica')]");

没有任何作用

<ul tabIndex="-1" class="k-list k-reset" id="ArrangementsWN_BusinessUnitId_listbox" role="listbox" aria-hidden="true" aria-live="off" style="height: 177px; overflow: auto;" data-role="staticlist" unselectable="on">
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="0">Jelenia Góra</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="1">Legnica</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="2">Opole</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="3">Wałbrzych</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="4">Wrocław</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="5">Bielsko Biała</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="6">Będzin</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="7">Częstochowa</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="8">Kraków</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="9">Tarnów</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="10">Obcy PGE</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="11">Obcy ENEA</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="12">Obcy ENERGA</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="13">Gliwice</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="14">Centrala</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="15">TD Serwis</li>
    <li tabIndex="-1" class="k-item" role="option" unselectable="on" data-offset-index="16">Wykonawca obcy</li>
</ul>

3 个答案:

答案 0 :(得分:0)

首先,您需要单击父元素,然后等待所需的列表项可见,然后单击它。我不熟悉C#,但Java实现将遵循这些方针:

driver.findElement(By.id("ArrangementsWN_BusinessUnitId_listbox")).click();
new WebDriverWait(driver, 10).until(visibilityOf(driver.findElement(By.xpath("//li[contains(text(), 'Legnica')])));
driver.findElement(By.xpath("//li[contains(text(), 'Legnica')]")).click();

答案 1 :(得分:0)

你可以这样做:

<android.support.v4.view.ViewPager
    android:id="@+id/viewPageAndroid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/aa"
    />

答案 2 :(得分:0)

如果您希望能够在c#中定义要单击的元素,可以这样做:

var options = _driver.FindElements(By.Class("k-item"));
driver.findElement(By.id("ArrangementsWN_BusinessUnitId_listbox")).click();

foreach(var element in elements) 
{
if(element.Text.Equals("option text")
  {
   element.click();
   }
}

我在代码i测试中使用它来进行大量的剑道下拉。它还允许我选择特定的选项,而不仅仅是第一个,因为我可以在&#34;选项文本中插入任何内容&#34;