Reputation: 13
I want to identify this element "Question With Text Response" which has no unique id, class or attribute.
I have tried this:
@FindBy(css = "body")
Can anyone please help me to locate this?
Here is the HTML:
<tr valign="top" xpath="1">
<td width="80%">
<div id="div-4fd242f670a4bc4e0170beb543d72c66" class="wordwrap" style="padding-left:20px;padding-top:3px;">
<label for="name" class="portlet-form-field-label">
2.
Question With Text Response<span style="color:red;">*</span>
</label>
<br>
<textarea id="4fd242f670a4bc4e0170beb543d72c66" name="answers[4fd242f670a4bc4e0170beb543d72c66]" class="portlet-form-input-field answers[4fd242f670a4bc4e0170beb543d72c66] comment" style="width:90%;" onkeydown="return imposeMaxLength(event, this, 3999);" onblur="return imposeMaxLength(event, this, 3999);" rows="7"></textarea>
</div>
</td>
</tr>
Upvotes: 0
Views: 50
Reputation: 7708
As per your HTML structure the label
and textarea
are sibling tags So to locate textarea based on label text,
Use below xpath:
@FindBy(xpath = "//label[contains(.,'Question With Text Response')]/following::textarea")
Explanation:
//label[contains(.,'Question With Text Response')]
- find label which contains text 'Question With Text Response'
/following::textarea
or /following-sibling::textarea
- locate the text are which follows the label
Upvotes: 0
Reputation: 4507
You can find the element using its text in the xpath.
You can do it like:
WebElement element = driver.findElement(By.xpath("//label[contains(text(),'Question With Text Response')]"));
Upvotes: 1