Dustin N.
Dustin N.

Reputation: 795

Finding Correct Nested Class For Input - Java - Selenium Webdriver

The page I am testing has text boxes for data entry, but they are nested within classes of the same name. How would I go about finding the correct one to know which one to enter the data?

Example code:

<span class="sectionWrapper"
   <div class="lineItem"
      <div class="Name" title="Fire" </div>
      <div class="itmeLimit"
         <input name="limit">
      </div>
   </div>
   <div class="lineItem"
      <div class="Name" title="Wind" </div>
      <div class="itmeLimit"
         <input name="limit">
      </div>
   </div>
   <div class="lineItem"
      <div class="Name" title="Hail" </div>
      <div class="itmeLimit"
         <input name="limit">
      </div>
   </div>
</span>

So basically, I need to enter the values on the input name="limit" field. How would I enter "20,000" for the title Wind?

Here's a snippet of actual code:

<span class="sectionWrapper" sort-order="27">
     <div class="linItm_tableTopWrap tableTopWrap_ifSubLine">
     <div class="subLine_tableRowsHeader">
<div class="linItm_tableRow subLine_tableRow" sort-order="28">
<div class="linItm_visibleRow visibleRow_ifSubLine">
<a class="linItm_accordionArrow subLine" href="#" style="display: none;"/>
<a class="remove removeLineItem" style="visibility: hidden" name="A. Dwelling - Fire" href="#"/>
<div class="linItm_lineItemName" title="A. Dwelling - Fire">A. Dwelling - Fire</div>
<div class="linItm_date">08/25/2015</div>
<div class="linItm_limit_wrap">
<div class="linItm_limit_dollarInput">
<input class="currencyInput contentRow_amountInput resetPremiums limitInput maxMin" type="text" style="text-align:right" value="0.00" name="limit"/>
<div class="inputDollarSign">$</div>
</div>
</div>
<div class="linItm_deductible_wrap">
<div class="linItm_proRataPremium_wrap contentRow_rataPremium black">
<div class="linItm_annualPremium_wrap black">
<input class="linItm_manualCheckbox" type="checkbox" name="manual"/>
<a class="view" href="#" style="display: none;"/>
</div>
<div class="linItm_accordionRow accordionRow_ifSubLine" style="height: 0px;"/>
</div>
     <div class="linItm_tableRow subLine_tableRow" sort-order="29">
     <div class="linItm_tableRow subLine_tableRow" sort-order="30">
     <div class="linItm_tableRow subLine_tableRow" sort-order="31">
     <div class="linItm_tableRow subLine_tableRow" sort-order="32">
     <div class="linItm_tableRow subLine_tableRow" sort-order="33">
     <div class="linItm_tableRow subLine_tableRow" sort-order="34">
     <div class="linItm_tableRow subLine_tableRow" sort-order="36">
     <div class="linItm_tableRow subLine_tableRow" sort-order="37">
     <div class="visibleRow_ifSubLine_bottomArea"/>
</span>

Upvotes: 1

Views: 4060

Answers (2)

Soggiorno
Soggiorno

Reputation: 774

Find all divs that can contain the input:

List<WebElement> lineItemDivs = driver.findElements(By.cssSelector(".lineItem"));

Iterate through the children of each div till an element with the appropriate attribute key/value is found. If such an element is found, find the input field of that specific div and type in the wanted value:

for (WebElement div : lineItemDivs)
{
    if (div.findElements(By.cssSelector("[title='Wind']")).size() > 0)
    {
        div.findElement(By.tagName("input")).sendKeys("20000");
    }
}

Upvotes: 1

alecxe
alecxe

Reputation: 473893

Keep it simple. You can use an XPath locator and locate the input in one go:

//div[contains(@class, "linItm_visibleRow") and div/@title="A. Dwelling - Fire"]//input[@name="limit"]

Upvotes: 0

Related Questions