I've got a field with a date in it that's identified by a code that's shown in the div previous to it.

<div class="fcode">
    <div class="fcode_n">(123)</div> 
    <div class="fcodeText">Date</div>
<div class="text">09.01.2020</div>

All the class names are generic and appear many times, but I can find the div class containing the code I'm after (123) by using

element = browser.find_element_by_xpath("//div[contains(text(),'151')]")

and can find the parent using

parent = element.find_element_by_xpath("..")

But I'm not stuck on how to find the date in the next element (which is what I'm after). The next element is , I suppose, the following sibling of the parent. There is a 'following_sibling' method like that gets you the sibling of something in your xpath, like this:


but that of course gets me the fcodeText element, not what I'm after - I blv. I need something in the form of


since I'm searching based on an element and not searching the whole document for the sibling following a given bit of text. I don't seem to find any docs on the use of ".." for finding a parent, but if I could it might have info on an analogous method for sibling instead of parent.

2 Answers 2


I think you can use parent:


In any automation project, correct selection of locators makes your automation framework more efficient. Xpath is one of the techniques commonly used in Selenium which enables easy, efficient and reliable automation. It allows tester to navigate through XML structure of any document. We can create xpaths specific to requirement which is a good practice followed often by most of the professional software testing services companies.

As mentioned in your case, where we need to find the date value using following-sibling parent relationship, below mentioned pattern will provide you required results - //div[text()='(123)']/../following-sibling::div/div

In generic, following-sibling can be written as - //tagname[@attribute='value']//following-sibling::tagname

This will select all nodes that have same parent as current node and also appears after it.

  • I can see the same company linked in almost every answer you posted. How is this relevant to this and other answers?
    – dzieciou
    Aug 15, 2021 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.