test123123132
test123123132

Reputation: 13

@FindBy search for element that has sub element which meets condition

Lets say i've got this html:

<table class="list">
    <tbody>
        <tr>
            <td>
                <a href="amazon">
            <td>
                <p> 10 Euros <p>
        <tr>
            <td>
                <a href="otherRetaile">
            <td>
                <p> 10 Euros <p>
        <tr>
            <td>
                <a href="otherRetailer1">
            <td>        
                <p> 11 Euros <p>
        <tr>
            <td>    
                <a href="otherRetailer2">
            <td>
                <p> 12 Euros <p>

and now i want to access the <p> that is in the same tr as the <a> tag with the href "amazon". What's the best approach to this, do i need a for loop over all those div's or can i use the @FindBy annotation? Do i need multiple @FindBy or a @FindAll to get all the divs in my list to check those for amazon?

Upvotes: 0

Views: 98

Answers (2)

Lia
Lia

Reputation: 526

Use xpath with /.. like that:

//table/tbody/tr/td/a[contains(@href,'amazon')]/../../td/p[1]

/.. alows you to reach parent of the current element

or shorter version

//td/a[contains(@href,'amazon')]/../..//p[1]

Upvotes: 1

Tim
Tim

Reputation: 91

almost the similar as @Lia answer, but a little different in searching element

element = driver.findElement(By.xpath("//a[@href='amazon']")); parent = element.findElement(By.xpath("./../../p"));

Upvotes: 0

Related Questions