Selenium Python I have an Xpath using Ancestor axis can i convert this to a CSS selector - python-2.7

I have an HTML page with a table of checkboxes. Each checkbox has a name.
In col1 is the checkbox. In col2 is the name.
I want to find the checkbox which has the name "Address"
I have managed to find it by using the following Xpath:
//table[#id="reporting_add_report_tab_manual_ct_vars_usn"]/tbody//tr//td//div[contains(text(), "Address")]//ancestor::tr[1]//input[#type="checkbox"]
I locate the column which contains the text "Address"
I then use ancestor to go up 1 row to it's parent of the current node and locate the checkbox
I would like to use CSS, can this xpath be converted to use the CSS locator?
CSS is quicker than Xpath to locate elements on the web page.
The HTML is:
<table id="reporting_add_report_tab_manual_ct_vars_usn" class="GJPPK2LBJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody style="">
<tr class="GJPPK2LBCD GJPPK2LBJD" __gwt_subrow="0" __gwt_row="0">
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED GJPPK2LBKD">
<div __gwt_cell="cell-gwt-uid-1204" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD">
<div __gwt_cell="cell-gwt-uid-1205" style="outline-style:none;">Name</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD">
<div __gwt_cell="cell-gwt-uid-1206" style="outline-style:none;">
<select tabindex="-1">
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD">
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD GJPPK2LBKD">
</tr>
<tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="1">
<td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBED">
<div __gwt_cell="cell-gwt-uid-1204" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDE">
<div __gwt_cell="cell-gwt-uid-1205" style="outline-style:none;">Address</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDE">
<td class="GJPPK2LBBD GJPPK2LBDE">
<td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBOD">
</tr>
<tr class="GJPPK2LBCD" __gwt_subrow="0" __gwt_row="2">
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED">
<div __gwt_cell="cell-gwt-uid-1204" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD">
<div __gwt_cell="cell-gwt-uid-1205" style="outline-style:none;">DOB</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD">
<td class="GJPPK2LBBD GJPPK2LBDD">
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD">
</tr>
<tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="3">
</tbody>
<tbody style="display: none;">
<tfoot style="" aria-hidden="false">
</table>

If the order of the checkboxes is fixed you can use nth-of-type
input[type='checkbox']:nth-of-type(2)
This will find the second occurrence of input[type='checkbox'].
Or you can use nth-child.
table#reporting_add_report_tab_manual_ct_vars_usn tr:nth-child(2) td input
This finds the second tr in the table, in that row a td with an input.

Related

Selenium XPATH i am trying to select a radio button from column 2 of table where text is name

I have a table of 5 columns. Column 2 has some text for the Name. Column 1 is a radio button. Column 3, 4 and 5 is a radio button.
I would like to select the radio button from column 3 where name text is "MaidenName"
I can find MaidenName then i tried to use following-sibling to go to the next column. My XPATH is not working.
My XPATH is:
//table[#id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//tr//td//div//span[text()="MAIDENNAME"]/following-sibling::td[2]//div//span[#type="radio"]
The HTML sample is:
<table id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body" cellspacing="0"
style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
# few trs here. I have removed. too long to paste otherwise
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="12">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
<td class="GJPPK2LBEG GJPPK2LBGG">
<div __gwt_cell="cell-gwt-uid-1076" style="outline-style:none;">
<span class="" title="DOB"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DOB</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<div __gwt_cell="cell-gwt-uid-1077" style="outline-style:none;">
<input type="radio" name="rb3386"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<div __gwt_cell="cell-gwt-uid-1078" style="outline-style:none;">
<input type="radio" name="rb3386"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
<div __gwt_cell="cell-gwt-uid-1079" style="outline-style:none;">
<input type="radio" checked="" name="rb3386"/>
</div>
</td>
</tr>
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="13">
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG">
<div __gwt_cell="cell-gwt-uid-1075" style="outline-style:none;">
<input type="radio" name="rbCrossRow119"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH">
<td class="GJPPK2LBEG GJPPK2LBFH">
<td class="GJPPK2LBEG GJPPK2LBFH">
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH">
</tr>
<tr class="GJPPK2LBFG GJPPK2LBMG" __gwt_subrow="0" __gwt_row="14">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1076" style="outline-style:none;">
<span title="MAIDENNAME"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">MAIDENNAME</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1077" style="outline-style:none;">
<input type="radio" name="rb3388"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1078" style="outline-style:none;">
<input type="radio" name="rb3388"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1079" style="outline-style:none;">
<input type="radio" checked="" name="rb3388"/>
</div>
</td>
</tr>
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="15">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="16">
</tbody>
</table>
Having found MaideName I would like to go down 2 columns and select the radio button in the 2nd column from MaidenName
Thanks, Riaz
I would stop at the tr point checking that there is MAIDENNAME text inside the appropriate column and then get the desired column in this row:
//table[#id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//
tr[td[2]/div/span/text()="MAIDENNAME"]/
td[3]/div/input[#type="radio"]
newlines added for readability.
Also note that I also had to fix the tag name - you are targeting the input element and not the span element.
I have worked it out now. I should use the following attribute. It is in the same tr row. following-sibling would go down to the next tr node.
Need to stay in the same node.
My working XPATH is:
//table[#id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body"]/tbody//tr//td//div//span[text()="MAIDENNAME"]/following::td[2]//div//input

Selenium Python There are 2 span tags in a html table td column, how do i get the 2 values out

I have a html table with some rows and columns.
In column 6 there are 2 span tags in a single td tag. Each span tag has a value.
The row in the table with the value 4 in column 6 and the value 24 in column 6
I would like to get the value 4 and 24 out where column 2 has the text all_matches_filtered and where column 4 has the text Do_Name and Do_Name2
In my Selenium Python script I will be verifying if the values 4 and 24 are there for the text all_matches_filtered and the text Do_Name and Do_Name2
What XPath can i use to locate the value 4 and 24 please?
I have made a start with an Xpath and it locates the value 4 for the text all_matches_filtered. I would like to include the text Do_Name which is in the same row as value 4
My Xpath is:
//table[#id="reporting_reports_ct_fields_body"]//tr//td//div//span[contains(text(), "all_matches_filtered")]/following::td[6]//div//span[(text()="4")]
The HTML snippet is (I have shortened the html otherwise it will be quite long):
<table id="reporting_reports_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG GJPPK2LBMG" __gwt_subrow="0" __gwt_row="0">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2169" style="outline-style:none;" tabindex="0">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2170" style="outline-style:none;">
<span class="linkhover" title="all_matches_filtered"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">all_matches_filtered</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2171" style="outline-style:none;">
<span class="" title="manual"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">manual</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2172" style="outline-style:none;">
<span class="linkhover block" title="Do_Name"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Do_Name</span>
<span class="linkhover block" title="Do_Name2"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Do_Name2</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2173" style="outline-style:none;">
<span class="" title="This is a matches report"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">This is a matches report</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2174" style="outline-style:none;">
<span class="" title="USN entities"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">USN entities</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2175" style="outline-style:none;">
<span title="16/03/2016 10:36:45"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">16/03/2016 10:36:45</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-2176" style="outline-style:none;">
<span class="block" title="" style="">4</span>
<span class="block" title="" style="">24</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG">
</tr>
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="2">
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="3">
</tbody>
Thanks, Riaz
First at all get right row for
out where column 2 has the text all_matches_filtered and where column 4 has the text Do_Name and Do_Name2
this would be:
/table[#id="reporting_reports_ct_fields_body"]//tr[
td[2]//span[contains(text(), "all_matches_filtered")] and
td[4]//span="Do_Name"
]
and form here the right column (for example)
/td[8]//span
which than is something like:
//table[#id="reporting_reports_ct_fields_body"]//tr[td[2]//span[contains(text(), "all_matches_filtered")] and td[4]//span="Do_Name"]/td[8]//span
try this code:
from selenium import webdriver
driver = webdriverFirefox() # driver = webdriverChrome()
driver.get('inser_page_URL_where_table_located')
table_data = driver.find_elements_by_xpath('//td[#class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG"]')[-1]
values = []
for i in table_data.find_elements_by_tag_name('span'):
values.append(i.text)
print values
and let me know if any exceptions occured

Selenium XPATH I am looking for an input field in a HTML table which has a specific text value

I have a HTML table with some rows of input fields. Each input field has a value.
The input field I am looking for is any input field which contains the text "filter"
The actual text is "filter2" or it could be "filter3"
Can i use the contains keyword in the Xpath to find "filter" for the #value attribute?
The XPATH i have constructed is:
//table[#id="reporting_add_report_tab_manual_ct_match_filters"]/tbody//tr//td//input[#value="filter2"]
I would like to find the input field where it has filter in the value attribute, exclude the number 2
The HTML snippet is:
<table id="reporting_add_report_tab_manual_ct_match_filters" class="GJPPK2LBAV border" cellspacing="0"
__gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody style="">
<tr class="GJPPK2LBCD GJPPK2LBMD" __gwt_subrow="0" __gwt_row="0">
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED">
<div __gwt_cell="cell-gwt-uid-684" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD">
<div __gwt_cell="cell-gwt-uid-685" style="outline-style:none;">
<span>
<input type="text" tabindex="-1" value="rule1"/>
</span>
<span>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD">
</tr>
<tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="1">
<td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBED">
<div __gwt_cell="cell-gwt-uid-684" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDE">
<div __gwt_cell="cell-gwt-uid-685" style="outline-style:none;">
<span>
<input type="text" tabindex="-1" value="filter2"/>
</span>
<span>
<div style="color:red;font-style:italic;"/>
</span>
</div>
</td>
<td class="GJPPK2LBBD GJPPK2LBDE GJPPK2LBOD">
</tr>
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>
</table>
Thanks,
Riaz
//table[#id="reporting_add_report_tab_manual_ct_match_filters"]/tbody/tr/td//input[#value!="filter2"][contains(#value,"filter")]

Selenium Python select the link from 3rd column from a table

I have a page with a table with some rows and columns. The first column has a checkbox (index is 0). The 2nd column has the name. The 3rd column has a link called view. There are some rows of data in the table.
I would like to click the view link which has the name "Selenium_CRM_For_Edit_Test"
The name could be anywhere in the table of rows.
I can identify the column name with the following XPATH:
//span[contains(., "Selenium_CRM_Edit_Test")]
I do not know how to get to the next column which has the view link
I have tried the following XPath:
//span[contains(., "Selenium_CRM_Edit_Test")]/../preceding-sibling::td/div/span[contains(text(), "view")]
That does not work. I thought preceding-sibling would go down to the next TD element
The HTML snippet is:
<table id="data_configuration_data_previews_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1">
<tr class="GJPPK2LBFG GJPPK2LBMG" __gwt_subrow="0" __gwt_row="18">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-237" style="outline-style:none;">
<span class="linkhover" title="Selenium_LADEMO_CRM_DONOTCHANGE" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Selenium_CRM_For_Edit_Test</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-238" style="outline-style:none;">
<span class="linkhover" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG">
</tr>
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="19">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="20">
</tbody>
How do i get to the link which has the name Selenium_CRM_For_Edit_Test?
Thanks,
Riaz
Assuming you want to locate the
<span class="linkhover" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
element, based on the text in
<span class="linkhover" title="Selenium_LADEMO_CRM_DONOTCHANGE" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Selenium_CRM_For_Edit_Test</span>
you can use the following axis:
//span[contains(., "Selenium_CRM_Edit_Test")]/following::span[#class="linkhover"]

Selenium Python my Xpath won't click the checkbox inside the table 1st column

I have a page with a table with some rows and columns. The first column has a checkbox (index is 0). The 2nd column has the name.
There are some rows of data in the table. I would like to click the checkbox which has the name "Selenium_CRM_For_Edit_Test"
My XPATH won't click the checkbox. I can locate the checkbox using XPATH checker in Firebug. My XPATH is:
//table[#id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "Selenium_CRM_For_Edit_Test")]/preceding::div[1]//../input
The HTML is:
<table id="data_configuration_data_previews_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="0">
<tr>.. some more rows here
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="15">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="16">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG">
<div __gwt_cell="cell-gwt-uid-1417" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<div __gwt_cell="cell-gwt-uid-1418" style="outline-style:none;">
<span class="linkhover" title="MegaOne_CHROME" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">MegaOne_CHROME</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBGG">
<td class="GJPPK2LBEG GJPPK2LBGG">
<td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH">
</tr>
<tr class="GJPPK2LBEH GJPPK2LBMG" __gwt_subrow="0" __gwt_row="17">
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBHG GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1417" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1418" style="outline-style:none;">
<span class="linkhover" title="Selenium_CRM_Edit_Test" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Selenium_CRM_Edit_Test</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1419" style="outline-style:none;">
<span class="linkhover" title="view" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">view</span>
</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1420" style="outline-style:none;">File</div>
</td>
<td class="GJPPK2LBEG GJPPK2LBFH GJPPK2LBBH GJPPK2LBNG">
<div __gwt_cell="cell-gwt-uid-1421" style="outline-style:none;">498</div>
</td>
</tr>
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="18">
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="19">
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="20">
</tbody>
</table>
My Selenium Python code to select the checkbox is:
def select_a_data_preview(self, data_preview):
# to get to the checkbox By.XPATH //table[#id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "Selenium_LADEMO_CRM_DONOTCHANGE")]/preceding::div[1]//../input
data_preview_element = self.driver.find_element(By.XPATH, '//table[#id="data_configuration_data_previews_ct_fields_body"]/tbody/tr//../td//../div/span[contains(text(), "%s")]/preceding::div[1]//../input' % data_preview)
data_preview_element.click()
return self
I have also tried looping through the table, find the name from col 1 and then click col 0 which is where the checkbox is. It does not work. My code snippet is:
def select_a_data_preview(self, data_preview):
try:
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
col_checkbox = row.find_elements(By.TAG_NAME, "td")[0] # This ist the checkbox col
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Configuration Name column
col_type = row.find_elements(By.TAG_NAME, "td")[3] # This is the Type column
col_rows = row.find_elements(By.TAG_NAME, "td")[4] # This is the Rows column
print "col_name.text = "
print col_name.text
print col_type.text
print col_rows.text
if (col_name.text == data_preview):
col_checkbox.click()
return True
return False
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("data_previews_page_select_element")
return False
return self
How can i click the checkbox that i want?
Thanks,
Riaz
Here you go:
//tr[td[contains(., "Selenium_CRM_Edit_Test")]]//input
Explanation:
First we look at the tr, then we look at the td with specific text that's in the previously looked at tr and after that we find the needed checkbox very easily.
OR
You can go from behind by looking at the span with text first and then going up and up by each level like so:
//span[#title="Selenium_CRM_Edit_Test"]/ancestor::tr//input