Selenium Python I want to check an element does not have a value I get the error NoSuchElementException: Message: Unable to find element with xpath - python-2.7

I have a HTML table with some rows and columns. I can get the value I want from for a row from column 3 which has the value "14"
When a user deletes a record from the GUI I would like to check that 14 is not present anymore.
I get the error:
NoSuchElementException: Message: Unable to find element with xpath == //table[#id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[#title="14"]
My XPATH to find the value is:
usn_id_element = self.get_element(By.XPATH, '//table[#id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[#title="14"]')
My function routine to check the value is not there is
def is_usn_id_not_displayed_in_all_records_report_results(self, usn_id): # When a record has been disconnected call this method to check the record for usn id is not there anymore.
usn_id_element = self.get_element(By.XPATH, '//table[#id="reporting_view_report_dg_main_body"]//tr//td[3]/div/span[#title="14"]')
print "usn_id_element"
print usn_id_element
print usn_id_element.text
if usn_id not in usn_id_element:
return True
get_element routine:
from selenium.webdriver.common.by import By
# returns the element if found
def get_element(self, how, what):
# params how: By locator type
# params what: locator value
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time. This way the screenshot name will be unique and be able to save
self.save_screenshot(screenshot_name)
raise
return element
The HTML snippet is:
<table id="reporting_view_report_dg_main_body" cellspacing="0" style="table-layout: fixed; width: 100%; margin-bottom: 17px;">
<colgroup>
<tbody>
<tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="0"\>
<tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="1"\>
<div __gwt_cell="cell-gwt-uid-9530" style="outline-style:none;">
<span title="14" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">14</span>
</div>
</td>
<td class="GFNQNVHIM GFNQNVHKM"\>
<td class="GFNQNVHIM GFNQNVHKM"\>
</tr>
<tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="13">
<td class="GFNQNVHIM GFNQNVHJN GFNQNVHLM">
<td class="GFNQNVHIM GFNQNVHJN">
<td class="GFNQNVHIM GFNQNVHJN">
<div __gwt_cell="cell-gwt-uid-9530" style="outline-style:none;">
<span class="" title="14" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">14</span>
</div>
</td>
<td class="GFNQNVHIM GFNQNVHJN"\>
<td class="GFNQNVHIM GFNQNVHJN"\>
</tr>
<tr class="GFNQNVHJM" __gwt_subrow="0" __gwt_row="14"\>
<tr class="GFNQNVHIN" __gwt_subrow="0" __gwt_row="15"\>
</tbody>
</table>
How can check if the value is not there?
Thanks, Riaz

Right now you are checking the attribute 'title' has a value of 14 and not the contents of the cell. What happens after the delete occurs? Does the span remain in the cell? Does the value of the cell becomes blank and does the value of the attribute 'title' also becomes blank?
The xpath below checks that the value of the cell is blank after deletion. Assumption you get a blank cell after deletion.
"//table[#id='reporting_view_report_dg_main_body']//tr//td[3]/div/span[.='']"
If you wanna check with value of title after deletion
"//table[#id='reporting_view_report_dg_main_body']//tr//td[3]/div/span[not(#title='14')]"

Related

Selenium Python I am trying to iterate rows in a html table I get error Index out of range

I have a webpage with a HTML table with some columns and rows. The user can select a row from the table and click the delete button. This will delete the row from the html table. E.g. In the Name column (index 1) there is a value "Testa"
The user can select the checkbox for the row where Name is "Testa" and delete it.
I would like to iterate over the rows and check "Testa" is not there. This way I can be sure the delete worked.
My code is not iterating over the table.
My code snippet is:
def is_value_deleted_from_user_defined_field(self, name): # return true if the user defined name is null. This method is to verify the user defined field has been deleted
# Params name : The name of the user defined field e.g. AREACODE, Testa
try:
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_edit_data_object_tab_details_tb_fields')))
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_edit_data_object_tab_details_tb_fields')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# 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 != name):
return True
return False
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("is_value_deleted_from_user_defined_field")
return False
My code to call the method is:
self.assertTrue(data_objects_edit_page.is_value_deleted_from_user_defined_field("Testa"), "User Defiend field Testa has not been deleted")
The HTML snippet is:
<table id="data_configuration_edit_data_object_tab_details_tb_fields" class="GFNQNVHJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody style="">
<tr class="GFNQNVHCD GFNQNVHMD" __gwt_subrow="0" __gwt_row="0">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHED GFNQNVHLD GFNQNVHND">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHND">
<div __gwt_cell="cell-gwt-uid-407" style="outline-style:none;">
<input id="" class="" type="text" style="color: black;" value="AREACODE"/>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHND">
<div __gwt_cell="cell-gwt-uid-408" style="outline-style:none;">
<select tabindex="-1">
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHOD GFNQNVHND">
<div __gwt_cell="cell-gwt-uid-409" style="outline-style:none;">
<input id="" class="" type="text" style="color: black;" value="50"/>
</div>
</td>
</tr>
<tr class="GFNQNVHCE GFNQNVHJD" __gwt_subrow="0" __gwt_row="1">
<td class="GFNQNVHBD GFNQNVHDE GFNQNVHED GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-406" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDE GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-407" style="outline-style:none;">
<input id="" class="" type="text" style="color: black;" value="VSEQ"/>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDE GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-408" style="outline-style:none;">
<select tabindex="-1">
<option value="Integer">Integer</option>
<option selected="selected" value="Text string">Text string</option>
<option value="Date/time">Date/time</option>
<option value="Floating point">Floating point</option>
</select>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDE GFNQNVHOD GFNQNVHKD">
</tr>
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>
</table>
The error I get is:
Traceback (most recent call last):
File "E:\test_runners 2 edit project - add more tests for Regression\selenium_regression_test_5_1_1\Regression_TestCase\RegressionProjectEdit_TestCase.py", line 951, in test_000033_Data_Objects_edit_ACVSEQ2_and_save_edited_changes
self.assertTrue(data_objects_edit_page.is_value_deleted_from_user_defined_field("Testa"), "User Defiend field Testa has not been deleted")
File "E:\test_runners 2 edit project - add more tests for Regression\selenium_regression_test_5_1_1\Pages\data_objects_edit.py", line 502, in is_value_deleted_from_user_defined_field
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name column
IndexError: list index out of range
How do I iterate over the table and check "Testa" or for example "AREACODE" is not in the table?
"AREACODE" is in the HTML snippet above. I think it is at index column 1
Thanks, Riaz
IndexError: list index out of range
Actually you're passing hard coded index without checking it's length to determine whether column present or not after deleting the column, that's why it is throwing exception which is absolutely correct because after deleting column from desired row there is no more desire column element present at provided index.
You should try to check column elements length instead to determine whether deleted column element exists or not as below :-
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
cols = row.find_elements(By.TAG_NAME, "td")
if len(cols) > 1 and cols[1].text == name :
return False
return True
It seems to be just getting the header of the table and stopping there. It won't go to the next line which is cols = row.find_elements(By.TAG_NAME, "td")
Actually you need to return True after for loop as below :-
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows :
cols = row.find_elements(By.TAG_NAME, "td")
if len(cols) > 1 and cols[1].text == name :
return False
return True
I could use self.driver.page_source and check in the page source if the text is there or not
If you just want to determine desire text exists on the page or not after delete, you can try as below instead :-
if len(driver.find_elements_by_xpath(".//table[#id='data_configuration_edit_data_object_tab_details_tb_fields']//*[text()= '"+name+"']")) > 0 :
return False
return True

Selenium Python how do I get all row values for each column from the html table

I have a HTMl table with some rows and columns. I would like to print the values for each row and each column. I am getting nothing printed. When i step through my code there is no values at the line:
cols = row.find_elements(By.TAG_NAME, "td") # Get all the cols
The HTML snippet is:
<table id="search_data_browser_ct_data_browser" class="GFNQNVHJE" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody>
<tr class="GFNQNVHCD GFNQNVHJD" __gwt_subrow="0" __gwt_row="0">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHED GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-193" style="outline-style:none;">
<span class="linkhover" title="31" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">31</span>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-194" style="outline-style:none;">1</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<div __gwt_cell="cell-gwt-uid-195" style="outline-style:none;">
<span class="linkhover" title="Mr|Batman|Bane|Male" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Mr|Batman|Bane|Male</span>
</div>
</td>
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHKD">
<td class="GFNQNVHBD GFNQNVHDD GFNQNVHOD GFNQNVHKD">
</tr>
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="1">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="2">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="3">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="4">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="5">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="6">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="7">
<tr class="GFNQNVHCD" __gwt_subrow="0" __gwt_row="8">
<tr class="GFNQNVHCE" __gwt_subrow="0" __gwt_row="9">
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>
</table>
My method is:
def is_results_displayed_in_data_browser(self):
try:
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'search_data_browser_ct_data_browser')))
rows = table_id.find_elements(By.TAG_NAME, "tr")
for row in rows:
# Get the columns
cols = row.find_elements(By.TAG_NAME, "td") # Get all the cols
print "Name col = "
print cols[1].text # This is the USN column
print cols[2].text # Match count col
#print cols[3].text # Source_fields col
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("is_results_displayed_in_data_browser")
return False
When i step through my code I can see there are values at the line:
rows = table_id.find_elements(By.TAG_NAME, "tr")
How do I get the values for each cell, each column?
The error i get is:
Traceback (most recent call last):
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\LADEMO_Matching_and_Reporting_TestCase\Lademo_Matching_and_Reporting_TestCase.py", line 496, in test_000008_simple_text_search
data_browser_page.is_results_displayed_in_data_browser()
File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\Reports\reports_data_browser.py", line 109, in is_results_displayed_in_data_browser
print cols[2].text # Match count col
IndexError: list index out of range
I have even tried:
col = row.find_elements(By.TAG_NAME, "td")[2]
print col.text
Could it be because there is a header in the first row?
I have it working now. I had to iterate over the columns. The working code is:
def is_results_displayed_in_data_browser(self):
try:
table_id = self.driver.find_element(By.ID, 'search_data_browser_ct_data_browser')
rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the rows in the table
for row in rows:
cols = row.find_elements(By.TAG_NAME, "td")
for col in cols:
print col.text
except NoSuchElementException, e:
print "Element not found "
print e
self.save_screenshot("is_results_displayed_in_data_browser")
Thanks, regards, Riaz

Selenium Webdriver Python clicking radio button is showing WebElement object is not callable

I am trying to click on a radio button on a webpage which I am automating in Selenium Webdriver Python.
When my code tries to click on the radio button it is showing the error:
TypeError: 'WebElement' object is not callable:
The full error is:
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\MatchConfigrationPage_TestCase.py", line 85, in test_add_match_configuration_possibles_name
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
File "C:\Webdriver\ClearCore\Pages\match_rules_tab.py", line 82, in click_selected_rule_radio_button
radio_button = self.driver.find_element(By.XPATH, '//table[#id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[#title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[#type="radio"]')
TypeError: 'WebElement' object is not callable
I can find the button using the following XPATH in Firefox XPATH checker.
//table[#id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[#title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[#type="radio"]
My method to call the button and click is as follows:
from selenium.webdriver.common.by import By
def click_selected_rule_radio_button(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
radio_button = self.driver.find_element(By.XPATH, '//table[#id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[#title="%s" and contains(text(), "%s")]//ancestor::tr[1]//input[#type="radio"]' (name, name))
self.driver.execute_script("arguments[0].click()", radio_button)
return self
the name parameter in the method it's value is "Name", %s in the code has the value "Name"
I have also tried the following:
def click_selected_rule_radio_button2(self, name):
# params name: The name of the data object to be selected for the match rule, e.g. Name, Address
#WebDriverWait(self.driver, 20).until(EC.presence_of_all_elements_located((By.ID, 'match_configuration_add_possible_tab_match_rules_ct_mapping_body')))
radio_button = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//table[#id="match_configuration_add_possible_tab_match_rules_ct_mapping_body"]//span[#title="Name" and contains(text(), "Name")]//ancestor::tr[1]//input[#type="radio"]')))
radio_button.click()
return self
From my TestCase class i call the method as follows:
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
code snippet of test case is as follows:
def test_add_match_configuration_possibles_name(self):
print "*** Test add Match Configuration Possibles - Name ***"
projectNavigator = project_navigator.ProjectNavigatorPage(self.driver)
possiblesPage = projectNavigator.select_projectNavigator_item("Possibles") # click Possibles from project navigator
possiblesPage.click_add_possibles_button()
possiblesPage.enter_possible_matches_name_and_description_from_details_tab("name_dob", "date of birth possible match rule")
possibles_match_rules_tab = possiblesPage.click_match_rules_tab()
possibles_match_rules_tab.click_possibles_match_rules_add_button()
possibles_match_rules_tab.enter_match_rule_name("name_dob")
possibles_match_rules_tab.click_selected_rule_radio_button("Name")
The HTML is:
<table id="match_configuration_add_possible_tab_match_rules_ct_mapping_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GPI5XK1CFG" __gwt_subrow="0" __gwt_row="0">
<td class="GPI5XK1CEG GPI5XK1CGG GPI5XK1CHG">
<div __gwt_cell="cell-gwt-uid-339" style="outline-style:none;" tabindex="0">
<input type="radio" name="rbCrossRow2" />
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CGG">
<div __gwt_cell="cell-gwt-uid-340" style="outline-style:none;">
<span class="" title="Name" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span>
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CGG GPI5XK1CBH">
<div __gwt_cell="cell-gwt-uid-341" style="outline-style:none;">
<input id="match_configuration_add_possible_tab_match_rules_cb_name" type="checkbox" />
</div>
</td>
</tr>
<tr class="GPI5XK1CEH" __gwt_subrow="0" __gwt_row="1">
<td class="GPI5XK1CEG GPI5XK1CFH GPI5XK1CHG">
<div __gwt_cell="cell-gwt-uid-339" style="outline-style:none;">
<input type="radio" name="rbCrossRow2" />
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CFH">
<div __gwt_cell="cell-gwt-uid-340" style="outline-style:none;">
<span class="" title="Address" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Address</span>
</div>
</td>
<td class="GPI5XK1CEG GPI5XK1CFH GPI5XK1CBH">
</tr>
<tr class="GPI5XK1CFG" __gwt_subrow="0" __gwt_row="2">
<tr class="GPI5XK1CEH" __gwt_subrow="0" __gwt_row="3">
</tbody>
Can anyone see what is wrong, why the radio button is not callable, it will not click it?
Thanks,
Riaz
You can use JavaScript to click radio button.Code is as follow:
driver=webdriver.Chrome('./chromedriver.exe')
driver.get('your URL')
time.sleep(10)
radioButton = driver.find_element_by_xpath("Radio Button Xpath") #like //input[#id='female']
driver.execute_script("arguments[0].click();", radioButton)
#driver.quit()

I have a table of rows with checkboxes in columns. How do i select a particular checkbox

I have the following HTML structure. A table with rows, the 2nd column has the text displayed e.g. Title (in row 1), FName (in row 2), SNAME (in row3), GENDER etc.
The 3rd column has a checkbox for each row.
I am trying to select a particular checkbox. E.g. my method will accept a parameter (the name of the text value in the row e.g. TITLE).
The method will select the checkbox for TITLE.
When i call the method again with parameter FNAME, the checkbox for FNAME will be clicked.
I am using Selenium Webdriver with Python
I have tried the following XPATH to identify the checkbox:
//span[#title="TITLE" and contains(text(), "TITLE")]/following-sibling::*
//span [text()="TITLE"]/../../preceding-sibling::td/div/input[#type="checkbox"]
These do not find the checkbox for the row called TITLE
I can get to the TITLE with the following XPATH.
//span [text()="TITLE"]
My code snippet is:
def add_mapping2(self, name):
try:
checkbox = self.driver.find_element(By.XPATH, '//span [text()="+name+"]/../../preceding-sibling::td/div/input[#type="checkbox"]')
checkbox.click()
except NoSuchElementException, e:
return False
return True
From my unittest.Testcase class I call the method as follows:
class MappingsPage_TestCase(BaseTestCase):
def test_add_mappings(self):
mappingsPage = projectNavigator.select_projectNavigator_item("Mappings")
mappingsPage.add_mapping2("TITLE")
mappingsPage.add_mapping2("SNAME")
The HTML is:
<table id="data_configuration_edit_mapping_tab_mappings_ct_mapping_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="0">
<tr class="GOFU2OVEH GOFU2OVGH GOFU2OVPG GOFU2OVMG" __gwt_subrow="0" __gwt_row="1">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHG GOFU2OVHH GOFU2OVAH GOFU2OVNG">
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHH GOFU2OVAH GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-792" style="outline-style:none;">
<span title="TITLE" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVFH GOFU2OVHH GOFU2OVBH GOFU2OVOG GOFU2OVAH GOFU2OVNG">
<div __gwt_cell="cell-gwt-uid-793" style="outline-style:none;" tabindex="0">
<input type="checkbox" checked="" tabindex="-1"/>
</div>
</td>
</tr>
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="2">
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVHG">
<div __gwt_cell="cell-gwt-uid-791" style="outline-style:none;">
<input type="radio" name="rbCrossRow124"/>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG">
<div __gwt_cell="cell-gwt-uid-792" style="outline-style:none;">
<span class="" title="FNAME" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">FNAME</span>
</div>
</td>
<td class="GOFU2OVEG GOFU2OVGG GOFU2OVBH">
<div __gwt_cell="cell-gwt-uid-793" style="outline-style:none;">
<input type="checkbox" tabindex="-1"/>
</div>
</td>
</tr>
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="3">
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="4">
<tr class="GOFU2OVEH" __gwt_subrow="0" __gwt_row="5">
<tr class="GOFU2OVFG" __gwt_subrow="0" __gwt_row="6">
more rows with names with checkboxes etc......
</tbody>
</table>
What XPATH could I use to get the checkbox for TITLE, FNAME etc?
I have the table ID "data_configuration_edit_mapping_tab_mappings_ct_mapping_body"
Maybe there is a way to start from the table ID and use a for loop to iterate through the rows and find the particular checkbox?
Thanks.
Riaz
you would use the following xpath expression
String xpath = "//span[#title = 'TITLE']/ancestor::tr[1]//input[#type = 'checkbox']"
What it does:
first search for a span element with your parameter (pls change 'TITLE' to the variable you are using)
then find the first ancestor element that is a tr-element
from there find the input element that is your checkbox within this tr-element
You could then refine to sth like this:
WebElement table = driver.findElement(By.id("data_configuration_edit_mapping_tab_mappings_ct_mapping_body"));
WebElement checkbox = table.findElement(By.xpath(xpath));
For the interest of others whom come across a similar problem. My Python code which I have used for the above answer is:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
def add_mapping(self, name):
wait = WebDriverWait(self.driver, 10)
try:
checkbox = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[#title = "%s"]/ancestor::tr[1]//input[#type = "checkbox"]' % name)))
#table_id = self.driver.find_element(wait.until(EC.element_to_be_clickable(By.ID, 'data_configuration_edit_mapping_tab_mappings_ct_mapping_body')))
#checkbox = table_id.find_element(By.XPATH, '//span[#title = "TITLE"]/ancestor::tr[1]//input[#type = "checkbox"]')
checkbox.click()
except NoSuchElementException, e:
return False
return True
The table id i commented out also works.

How to extract data by matching a variable with the tag value in python

!--This is the first table from where i get 4 id's (abc1---abc4) which i need to match with the table below and get the required data--!
<table width="100%" border="0" class=""BigClass">
<tbody>..</tbody>
</table>
!--This is the second table --!
<table width="100%" border="0" class=""BigClass">
<tbody>
<tr align="left">
<td valign="top" colspan="2">
<strong> 1.
First Topic
</strong>
<a name="abc1" id="abc1"></a>
</td>
</tr>
!--This is the place where the first speaker and his/her text comes---!
<tr align="left">
<td style="text-align:justify;line-height:2;padding-right:10px;" colspan="2">
<strong> " First Speaker " </strong>
<br>
" Some Text "
</td>
</tr>
!--This is where the second speaker comes in---!
<tr align="left">
<td style="text-align:justify;line-height:2;padding-right:10px;" colspan="2">
<strong> " Second Speaker " </strong>
<br>
" Some Text "
</td>
</tr>
<tr><td colspan="2"><br></td></tr>
<tr><td colspan="2"><br></td></tr>
!--Then here comes the row with another id--!
<tr align="left">
<td valign="top" colspan="2">
<strong> 2.
Second Topic
</strong>
<a name="abc2" id="abc2"></a>
</td>
</tr>
!--Just like before, this will also have set of speakers who have some text--!
I have two tables with the same class name which is BigClass. From the first table i extracted 4 ids which are abc1,abc2,abc3,abc4.
Now i want to check that if these ids is present in this second table(which it is)
after it matches with the ids in the second table, i want to extract the speakers and the text of those speakers.
You can see the code structure for the second table rom which i want to extract the data.
It seems the best way to extract speaker and text information is to extract all ids in a list and all speaker info in another list. Then just cross-reference the ids needed and get the corresponding speaker info.
I create a dictionary here with key as ids and value as speaker info. I found the speaker info by the condition that the td field has a style attribute defined in all fields containing speaker info.
For extracting info from HTML, I am using the BeautifulSoup library.
from bs4 import BeautifulSoup
from itertools import izip
soup = BeautifulSoup(open('table.html'))
idList = []
speakerList = []
idsRequired = ['abc1','abc2']
for a in soup.findAll('a'):
if 'id' in a.attrs.keys():
idList.append(a.attrs['id'])
for i in soup.findAll('td'):
if 'style' in i.attrs.keys():
speakerList.append(i.text)
for key,value in izip(idList,speakerList):
if key in idsRequired:
print value
This gives me the output as:
" First speaker "
" Some text "
" Second speaker "
" Some text "