0

I have following structure on my web-site.

//New Box
<div>
    <div>
    </div>
    <div>
        <div>
            <div>
            </div>
            <div>
                <div>
                    <p>Example 777
                    </p>
                    <div>
                    </div>
                </div>
            </div>
            <div>
            </div>
            <div>
                <div>
                    <div>
                        <button>Click me
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>  
</div>

//New Box
<div>
    <div>
    </div>
    <div>
        <div>
            <div>
            </div>
            <div>
                <div>
                    <p>Example 321
                    </p>
                    <div>
                    </div>
                </div>
            </div>
            <div>
            </div>
            <div>
                <div>
                    <div>
                        <button>Click me
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>  
</div>

A lot of "Boxes" of this structure. No id, no classes

I have following tasks:

  1. Click on a button, where there is a p with "Example 777"
  2. Find first Box and click a button in it.
  3. Find last Box and click a button in it.

I have no idea how to do the first task.

According 2 and 3, @FindBy uses constant String, so should I use separate @FindBy for the first and last Box, even if the code is deprecated?

1

@FindBy will only locate one or multiple elements by a static string, with no way to provide arguments that I know of.

I would recommend creating this as a method rather than a @FindBy annotation, as you can easily use the same locator code to work with identical structures in the HTML, e.g.

public WebElement findBoxButton(String pText) {
    return driver.findElement(By.xpath("//p[contains(text(), '" + pText + "')]/../../..//button"));
}

See how it allows you to find any button based on a P with particular text?

Breakdown of this xpath:

  1. Find P with the text we want
  2. Go back a bunch until we're at the parent of both the p and the related button
  3. Find the first button anywhere within the node we're currently at

If I've misinterpreted this, and you only want to interact with the first and last boxes, and you don't want to find them via the P text, you should be able to do this if there are no buttons outside of boxes:

@FindBy(xpath="//button")
List<WebElement> boxButtons;

Then get the first and last elements from this list.

boxButtons.get(0); // returns a WebElement
boxButtons.get(boxButtons.size() - 1); // returns a WebElement

Also please consider asking your developers for some IDs (if this is a website developed in-house). Xpath without meaningful elements to navigate via is brittle and hard to read when it comes time for maintenance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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