Issue
Below is the full HTML tag where I want to use xpath with multiple conditions.
<div class="request-body comments-left-discription clickme" style="cursor:pointer; " id="12340" title="" data-placement="bottom" data-toggle="tooltip" mode="">
<div class="desc-title">
<a target="_blank" class="site-color" href="http://test://0316">Nutan</a> requested <a href="http://test://0316">Approval</a></div>
<div class="msg-time"><small><a rel="tooltip" data-placement="right" title="" class="color6" href="#" data-original-title="Created on, ">ago</a> (020 )</small></div>
Auto <br>
<div class="btn" id="button">
<a id="accept" class="b" href="javascript:void(0);">APPROVE</a><a id="reject" class="btn" href="javascript:void(0);">REJECT</a> </div>
</div>
In the above HTML I want to use the below conditions in one xpath statement and I have written like this below and it shows unable to locate element.
driver.findElements(By.xpath("//div[starts-with(@id,'1234')"
+ " and //a[contains(text(),'off')]]]"));
Solution
a
tag is beneath the div
tag, so you can use the given below relative xpath for this:
driver.findElement(By.xpath("//div[contains(@id,'5e9b7')]//a[contains(text(),'Compensatory-off Approval')]"))
Updated answer after discussion with OP:
List<WebElement> elementList = driver.findElements(By.xpath("//div[contains(@id,'5e9b7')]//a[contains(text(),'Compensatory-off Approval')]"));
if(elementList.size()>0){
// Element is present
// Do the required operations here
}
Answered By - Sameer Arora