0

Let's say I have the following HTML layout:

<div>
    <button text='main' .../>
    <button .../>
</div>

Now let's say that the task is to find the button with text 'main' then go to the first ancestor div node and then go back down to the same 'main' button.

If I write the following:

find_elements(By.XPATH, "//button[text()='main']//ancestor::div[1]//child::button[1]")

It returns both buttons that are children of the div. Why is that?

It returns the first button (which actually I need), only if I use the absolute path in the end:

find_elements(By.XPATH, "//button[text()='main']//ancestor::div[1]/child:button[1]")

Is it possible to get the first button going down the hierarchy relying on the relative path?

Maybe I don't understand something, but it seems like magic for me. If we remake the layout like this:

<div>
    <button text='main' .../>
    <button .../>
    <a ...>
        <button />
    </a>
</div>

and then take children of div like this:

find_elements(By.XPATH, "//button[text()='main']//ancestor::div[1]//child::button")

it returns all three buttons, despite that I have read in a lot of articles that //child returns immediate child! What is going on here?

lutar
  • 43
  • 4

0 Answers0