java- webdriver单击列表元素

时间:2014-01-15 13:46:38

标签: java html list selenium

<div class="class0">
  <div class="class1">
    <input tabindex="4" id="pqr" autocomplete="off" type="text">
  </div>

  <ul class="class2">
    <li class="group-result">polka</li>
    <li class="active-result group-option" style="" data-array-      index="1">abcd</li>
    <li class="active-result group-option" style="" data-array-index="2">efgh</li>
    <li class="group-result">ijkl</li>
    <li class="active-result group" style="" data-array-index="3">abcd</li>
    <li class="active-result group" style="" data-array-index="4"></li>
    <li class="active-result" style="" data-array-index="5">xyz</li>
  </ul>

请原谅我,如果我不能在这里重现整个代码。在上面的html中,提示用户输入输入框并生成自动建议。你在上面看到的是在盒子里打字后得到的东西。

我需要点击一个li元素,比如说xyz。一种方法是获取ul元素,然后获取所有子元素。然后迭代它们并通过检查它们的内部文本来选择正确的一个。所以,我试过

WebElement a1 = driver.findElement(By.className("class0"));
WebElement a2 = a1.findElement(By.className("class2"));

List <WebElement> a3 = a2.findElements(By.xpath(".//*));
System.out.println(a3.size());

正如此问题的回复中所述:How to get all descendants of an element using webdriver?

但是print语句打印0,即没有找到孩子

有人可以建议为什么吗?

此外,任何其他方法建议都将受到欢迎。

由于

1 个答案:

答案 0 :(得分:0)

您应该等到DOM中存在class0 div,然后再查询其子项:

WebDriverWait wdw = new WebDriverWait(webDriver, 20);
WebElement a1 = wdw.until(ExpectedConditions.presenceOfElementLocated(By.className("class0")));

WebElement a2 = a1.findElement(By.className("class2"));

List <WebElement> a3 = a2.findElements(By.xpath(".//*));
System.out.println(a3.size());