id中的多个字段 - Webdriver - Java

时间:2015-08-13 13:36:44

标签: java selenium selenium-webdriver

我需要抓取的输入字段在@id="contractorsWrapper"

之内

在此示例中,该包装器中有2个输入字段(但这个数字是动态的,具体取决于具体情况)位于@class="contactEntry"

我尝试做的是说className=contactEntry中有多少id=contractorsWrapper个字段。然后,能够独立地输入文本。

<div id="contractorsWrapper" class="contactInputAndInfoDisplays_wrapper">
  <div id="contractorsRow_5d1532ba-b37e-4aac-85c2-4a5e6c6c2796" class="contactInputAndInfoDisplay">
    <div class="contactName">
      <div class="contactFlag"/>
      <a class="smallRemove removeAContact" href="#"/>
      <span class="littleGreyTitles">
        Name
        <br/>
      </span>
      <input class="contactEntry " type="text" value=""/>
    </div>
    <div class="descriptionInput littleGreyTitles">
      Description
      <br/>
      <input type="text"/>
    </div>
    <a class="contactLink" href="#" style="display: none;"/>
  </div>
  <div class="spacerDiv1"/>
  <div id="contractorsRow_5fc58f1a-906f-4239-93ae-b0a2e4b8b70c" class="contactInputAndInfoDisplay">
    <div class="contactName">
      <div class="contactFlag"/>
      <a class="smallRemove removeAContact" href="#"/>
      <span class="littleGreyTitles">
        Name
        <br/>
      </span>
      <input class="contactEntry " type="text" value=""/>
    </div>
    <div class="descriptionInput littleGreyTitles">
      Description
      <br/>
      <input type="text"/>
    </div>
    <a class="contactLink" href="#" style="display: none;"/>
  </div>
  <div class="spacerDiv1"/>
</div>

1 个答案:

答案 0 :(得分:2)

找到你的包装器:

WebElement wrapperElement = driver.findElement(By.id("contractorsWrapper"));

输入元素数量:

wrapperElement.findElements(By.className("contactEntry ")).size();

我不知道你的意思是“单独输入文字”,但这里是你如何在所有这些中输入相同的东西:

for (WebElement element : wrapperElement.findElements(By.className(className))) {
            element.sendKeys("keysToSend");
};

更新

来自OP的更多细节

如果要向元素插入一些“唯一”字符串,可以使用ArrayList

// create as much array entries as you need

List<String> namesList = new ArrayList<String>();
namesList.add("John Doe");
namesList.add("Jane Doe");
...

//然后

int count = 0;
for (WebElement element : wrapperElement.findElements(By.className(className))) {
            element.sendKeys(namesList.get(count++));
};

当然,您需要确保您的列表总是比输入元素的数量更长...