user2682459
user2682459

Reputation: 1029

Clicking element in table based on condition on another element in the same row

I have the following example structure for a table I'm looking at:

<tbody>
    <tr class="OddRow">
        <td class="TimeField">
        7:20 AM
        </td>
        <td class="TextField"></td>
        <td id="Price_2_1" class="MoneyField"></td>
        <td class="LinkField">
            <a id="basketControl_2_1" class="sr_AddToBasket">
                Add to Basket
            </a>
        </td>
    </tr>
    <tr class="EvenRow">
        <td class="TimeField"></td>
        <td class="TextField"></td>
        <td id="Price_2_2" class="MoneyField"></td>
        <td class="LinkField"></td>
    </tr>
    ...OddRow
    ...EvenRow
</tbody>

What I would like to be able to do is click (using selenium) on the element with class "sr_AddToBasket" when the value in class "TimeField" is something I specify. Only one row of the table can ever have the specified time in a particular instance.

I'm really stuck as to how to go about this so any help will be greatly appreciated! If it helps I'm currently attempting this in python, but have some knowledge of java.

Upvotes: 0

Views: 1520

Answers (3)

Karlos Dev
Karlos Dev

Reputation: 21

you can use the following: This question were answered here http://patch-recepteur.blogspot.com/2017/04/forum-clicking-element-in-table-based.html

<table>
<tbody>
    <tr class="OddRow">
        <td class="TimeField">
        7:20 AM
        </td>
        <td class="TextField"></td>
        <td id="Price_2_1" class="MoneyField"></td>
        <td class="LinkField">
            <a id="basketControl_2_1" class="sr_AddToBasket">
                Add to Basket
            </a>
        </td>
    </tr>
    <tr class="EvenRow">
        <td class="TimeField"></td>
        <td class="TextField"></td>
        <td id="Price_2_2" class="MoneyField"></td>
        <td class="LinkField"></td>
    </tr>

    <tr class="OddRow">
        <td class="TimeField"></td>
        <td class="TextField"></td>
        <td id="Price_2_1" class="MoneyField"></td>
        <td class="LinkField"></td>
    </tr>
    <tr class="EvenRow">
        <td class="TimeField">
        9:00 PM
        </td>
        <td class="TextField"></td>
        <td id="Price_2_2" class="MoneyField"></td>
        <td class="LinkField">
            <a id="basketControl_2_1" class="sr_AddToBasket">
                Add to Basket
            </a>
        </td>
    </tr>
</tbody>
</table>

Upvotes: 0

Subh
Subh

Reputation: 4424

Suppose your structure looks like below after you add two more rows:

<table>
<tbody>
    <tr class="OddRow">
        <td class="TimeField">
        7:20 AM
        </td>
        <td class="TextField"></td>
        <td id="Price_2_1" class="MoneyField"></td>
        <td class="LinkField">
            <a id="basketControl_2_1" class="sr_AddToBasket">
                Add to Basket
            </a>
        </td>
    </tr>
    <tr class="EvenRow">
        <td class="TimeField"></td>
        <td class="TextField"></td>
        <td id="Price_2_2" class="MoneyField"></td>
        <td class="LinkField"></td>
    </tr>

    <tr class="OddRow">
        <td class="TimeField"></td>
        <td class="TextField"></td>
        <td id="Price_2_1" class="MoneyField"></td>
        <td class="LinkField"></td>
    </tr>
    <tr class="EvenRow">
        <td class="TimeField">
        9:00 PM
        </td>
        <td class="TextField"></td>
        <td id="Price_2_2" class="MoneyField"></td>
        <td class="LinkField">
            <a id="basketControl_2_1" class="sr_AddToBasket">
                Add to Basket
            </a>
        </td>
    </tr>
</tbody>
</table>

So, now say you want to click on the 2nd link, then please use the below Java code:

driver.findElement(By.xpath("//td[@class='TimeField' and contains(text(),'9:00 PM')]/following-sibling::td/a[@class='sr_AddToBasket']")).click();

You can replace the '9:00 PM' above with your value.

Upvotes: 2

Saifur
Saifur

Reputation: 16201

if driver.find_elements_by_css_xpath("//tr[@class='EvenRow']/td[@class='TimeField']").get_attribute("value")=="your value":
            driver.find_element_by_xpath("//a[@class='sr_AddToBasket']").click()

this get_attribute should return you the value you set and simple if condition matches the criteria of execution. Then next you find the link you want and click. I used xpath since it's helps us to identify element in a table easily.

Upvotes: 0

Related Questions