0

I have a html table with some rows and columns. Each row has a checkbox in the 1st column. Name in the 2nd column. I want to select the checkbox which has the name test2 (i will use test2 as an example) I have tried using a for loop to loop through the table and rows and click the checkbox if the name test2 is found. My code is not clicking the checkbox.

My code snippet is:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

def select_datamap(self, datamap_name): 
        try:
            WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'td')))
            table_id = self.driver.find_element(By.ID, 'data_configuration_datamaps_ct_fields_body')
            rows = table_id.find_elements(By.TAG_NAME, "tr")
            for row in rows:
                print row
                # Get the columns
                col_name = row.find_elements(By.TAG_NAME, "td")[1]  # This is the Name column
                print col_name.text
                if (col_name.text == datamap_name):
                    checkbox = self.driver.find_element(By.XPATH, '//table[@id="data_configuration_datamaps_ct_fields_body"]/tbody/tr[%s]/td[1]/div/input') % row
                    #checkbox = self.driver.find_element_by_css_selector("#data_configuration_datamaps_ct_fields_body input[type='checkbox']")
                    checkbox.click()
                    return True
            return False
        except NoSuchElementException, e:
            return False

The HTML is:

<table id="data_configuration_datamaps_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
    <tr class="GOFU2OVFG GOFU2OVMG" __gwt_subrow="0" __gwt_row="0">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG GOFU2OVNG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="DM" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">DM</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVNG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH GOFU2OVNG">
    </tr>
    <tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="1">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG">
            <div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVFH">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="test1" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test1</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVFH">
        <td class="GOFU2OVEG GOFU2OVFH GOFU2OVBH">
    </tr>
    <tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
            <div __gwt_cell="cell-gwt-uid-317" style="outline-style:none;">
                <input type="checkbox" tabindex="-1"/>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG">
            <div __gwt_cell="cell-gwt-uid-318" style="outline-style:none;">
                <span class="linkhover" title="test2" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">test2</span>
            </div>
        </td>
        <td class="GOFU2OVEG GOFU2OVGG">
        <td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
    </tr>
</tbody>
</table>

from my TestCase class I call the select_datamap method with the parameter "test1"

import unittest
some more imports ...

class DatamapsPage_TestCase(BaseTestCase):

    def test_delete_datamaps(self):
        print "*** Test Delete DM Datamaps ***"
        some steps ...
        ...
        tool_bar = ToolbarPage(self.driver)
        dm = tool_bar.clickMoreTools("Data Configuration", "Datamaps" ) 
        dm.select_datamap("test1")

How can find the checkbox which matches the parameter i pass and then click it? Using the for loop would be good because the parameter test2 could be in any row depending on what names the client enters in the application.

Thanks,

1 Answer 1

0

Actually you could use following xpath to find the checkbox directly without iterating:

//span [text()='test2']/../../preceding-sibling::td/div/input[@type='checkbox']

Still the xpath-expression is not very nice but it should work :)

and then just make a .click() on the result

Sign up to request clarification or add additional context in comments.

Comments

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.