Referencing unnamed element with Selenium WebDriver

Mitch

I'm new to selenium, and I'm having trouble finding particular elements on a webpage with the WebDriver (in Java). The particular elements I am trying to reference are links, but do not have any ID/ other XML attributes associated with them. I do not have access to the HTML source code, so I cannot add any identifiers to the elements. I'm not exactly certain what can and can't be done using the FindBy functions, so can somebody take a look at the HTML and tell me how I might find the specified element (with the end goal being to use the WebElement.Click() method on it).

<div class="col-md-12">
    <h3>My Registered Cards</h3>
    <div id="cards" class="row list-group">
        <div>

THIS LINE

          <a href="/website/carddetails?cardnumber=1">

THIS LINE

                <div class="tile col-md-3 img-rounded">
                    <div class="tile-inner">
                        <img class="group list-group-image col-md-12 hidden-xs hidden-sm" src="http://placehold.it/200x100/000/fff" alt="">
                        <div>
                            <div class="row">
                                <h4 class="group inner list-group-tile-heading col-xs-12 col-md-12">Card Name</h4>
                            </div>
                        </div>
                    </div>
                </div>
            </a>

        </div>
        <div>

THIS LINE

          <a href="/website/carddetails?cardnumber=2">

THIS LINE

                <div class="tile col-md-3 img-rounded">
                    <div class="tile-inner">
                        <img class="group list-group-image col-md-12 hidden-xs hidden-sm" src="http://placehold.it/200x100/000/fff" alt="">
                        <div>
                            <div class="row">
                                <h4 class="group inner list-group-tile-heading col-xs-12 col-md-12">Card Name</h4>
                            </div>
                        </div>
                    </div>
                </div>
            </a>

        </div>
    </div>
</div>

I already tried to do it a couple different ways, but the WebDriver did not find the correct element in both scenarios.

@FindBy(linkText = "Card Name")
    public List<WebElement> cards;

and

@FindBy(xpath = "//div[@id='cards']/div/@a")
    public List<WebElement> cards;

EDIT: Thanks to SiKing Not exactly the answer I was looking for, but you did point me in the right direction.

What I actually ended up doing was

@FindBy(xpath = "//div[@id='cards']/div/a")
    public List<WebElement> cards;

If it was unclear, what I was trying to do was get a list of all the cards (specifically, card links) on the website that I could then click in sequence

SiKing

The selector

@FindBy(linkText = "Card Name")

Is going to find nothing, because it looks for that text enclosed in an a tag.

The selector

@FindBy(xpath = "//div[@id='cards']/div/@a")

Is looking for the attribute a of the element div, under the element div that has the attribute id='cards' ... so not what you want either.

You probably want:

@FindBy(xpath = "//div[@id='cards']//a")

Which will find the first a element under the div with attribute id='cards'.

Hey, if all else fails, try the documentation. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related