1

I have site and the HTML looks like this:

<tr role="row" class="odd">
    <td class="sorting_1">555</td>
    <td>
        <a href="/fruit_list/555">FruitType1 : Fruit1</a>
    </td>
    <td>Fruit1</td>
    <td>FruitType1</td>
    <td>Somwhere</td>
    <td></td>
    <td>0</td>
    <td>
        <button class="copy_button btn_gray_inverse" id="555">Copy</button>
    </td>
    <td>
        <button class="fruit check_btn" id="555" href="" value="0">
            <i class="bt_check"></i>
        </button>
    </td>
    <td>
        <a class="fruit remove_btn" id="555" href="#">
            <i class="bt_remove">
            ::before
            </i>
        </a>
    </td>
</tr>

Im trying to click on button (<button class="fruit check_btn" id="555" href="" value="0">) inside this specific row. Rows can be different only in text under tr (Fruit1, Fruit2), with this code and its not working:

FruitList = self.driver.find_elements_by_xpath\
    ('//tr[@role="row" and contains (., "Fruit1")]')
for Fruit in FruitList:
    Enabled = Fruit.find_element_by_xpath('//i[@class="bt_check"]')
    Enabled.click()

It allways clicks on button from first awailable row on the page, not the one that is containing text "Fruit1".

Please help

4
  • Can you update the question with your exact manual steps which you are trying to Automate? Commented Nov 4, 2017 at 12:16
  • 1. Locating row which contains text "Fruit1" 2. Click on button that is located inside row from step 1 Commented Nov 4, 2017 at 12:20
  • 1. What do you want to do after locating the row which contains text "Fruit1"? 2. Does this item row containing text "Fruit1" appears multiple times on the Web Page? Commented Nov 4, 2017 at 12:23
  • Nope, row containing "Fruit1" appears only once on a page. Commented Nov 4, 2017 at 12:26

2 Answers 2

1

To find the check button for Fruit1, I suggest you to do it in two steps.

First find the row for Fruit1:

fruit_row = driver.find_element_by_xpath("//td[text()='Fruit1']/..")

Note that I use /.. at the end of the Xpath in order to select the tr element that contains the td with text 'Fruit1' instead of the td himself.

Second step, find the button and click on it:

fruit_row.find_element_by_class_name("check_btn").click()
Sign up to request clarification or add additional context in comments.

Comments

0

To click on <td> with text as Fruit1, you can use the following line of code :

driver.find_element_by_xpath("//td/a[contains(@href,'/fruit_list/555')]//following::td[text()='Fruit1']")

You can be more generic with :

driver.find_element_by_xpath("//td/a[contains(@href,'/fruit_list/555')]//following::td[1]")

3 Comments

Yea thanks, but still clicks button frm first row on page, not the one that contains text "Fruit1".
New on stack so need to be more careful on which answer i comment. Thank you :)
My Answer was in Java earlier which I have changed to Python now. Check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.