Find xpath nearest element

Issue

I need to define an xpath before an element on the page. I have a string(FIO) that I can find using xpath and I need to bind to it. I don’t understand how to do it.

My xpath witch i can find on page:

/html/body/div[1]/div[2]/section/div/div[1]/div/ul/li[2]//div[1]/span[contains(., '"+FIO+"')]

look at screenshot, i need find string 1, it have xpath:

/html/body/div[1]/div[2]/section/div/div[1]/div/ul/li[2]/ul/li[4]/ul/li[1]/div/div/a

image

string with my param(FIO) 2, have xpath:

/html/body/div[1]/div[2]/section/div/div[1]/div/ul/li[2]/ul/li[4]/ul/li[1]/div/div/div[1]/span

and i shortened it and inserted a variable:

/html/body/div[1]/div[2]/section/div/div[1]/div/ul/li[2]//div[1]/span[contains(., '"+FIO+"')]

how i can get xpath to element 2 with binding at element 1 ? maybe following sibling ?

sorry, i can’t copy the code correctly, only like this:

                    </div>
</div>
        <ul>
                        <li>
<div class="structure2__item1">
    <div class="structure2__item2" style="">
         <a class="structure2__position" href=https://**>
               "String 2"
            </a>
            <div class="structure2__name" style="">
                  <span>String_FIO</span>
            </div>                                 
                </div>
</div>
</li>
                        <li>

Solution

//div[child::span[contains(text(), "String_FIO")]]/preceding-sibling::a

This would help fetch the a tag from the span.

(From next time – please look out for the standards mentioned in the comments.)

Answered By – Gowthaman Ravindran

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published