0

Html block first:

<div class="formClass" style="line-height: 18px; display: block;">

    <label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
        <input type="checkbox" value="30078" disabled="">First Text
    </label>

    <label id="randomId_34_30077" style="display: none; width:47%; margin-right: 5px;">
        <input type="checkbox" value="30077" disabled="">Second Text
    </label>
    
    <label id="randomId_32_30078" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
        <div style="display: inline-block; width: 10%; float: left;">

             <input type="checkbox" value="30078" disabled="" checked="">

        </div>
        <div style="display: inline-block; width: 90%; float: right;">
            First Text
        </div>
    </label>

    <label id="randomId_32_30077" style="display: inline-block; width:47%; margin-right: 5px; vertical-align: top;">
        <div style="display: inline-block; width: 10%; float: left;">
            
            <input type="checkbox" value="30077" disabled="">
        </div>
        <div style="display: inline-block; width: 90%; float: right;">
            Second Text
        </div>
    </label>

</div>

I'd like to get the checkbox of which the value is "30078" in the above HTML section. The id of labels are random, also the value of the input/checkbox. It's a legacy project. I can't modify the structure.

I tried this:

driver.findElement(By.className("formClass"))
                    .findElement(By.xpath("//label//div[contains(text(), 'First Text')]"))

but this gets two elements.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tiee
  • 121
  • 1
  • 9

2 Answers2

0

If you need to get the checkbox, you need to write the XPATH for the tag.

If you need to write the xpath for below :

 <label id="randomId_34_30078" style="display: none; width:47%; margin-right: 5px;">
    <input type="checkbox" value="30078" disabled="">First Text
</label>

you can use

//div[@class="formClass"]/label/input[contains(text(),"First Text")]
debugger89
  • 2,108
  • 2
  • 14
  • 16
  • findElements(By.xpath("//div[@class='formClass']//label//div[contains(text(),'First Text')]//preceding-sibling::div//input")) Thanks. I tried this. It worked. – Tiee Aug 23 '20 at 21:37
0

Though there are two elements with attribute value="30078" among them only one is visible while the other one contains the attribute style="display: none;.

So to get the visible/interactable checkbox with value attribute set as 30078 you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[id^='randomId'] input[value='30078']")));
    
  • xpath:

    WebElement element = new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(@id, 'randomId')]//input[@value='30078']")));
    

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks. The id and the value of the label and input are random, I can get the element by this xpath. – Tiee Aug 23 '20 at 21:22