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

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.

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 I want to check an element does not have a value I get the error NoSuchElementException: Message: Unable to find element with xpath

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')]"

Selenium Webdriver Python How do you wait for text of an element to appear after clicking a button and keep trying until text appears

I have a html table of with some columns. Each row shows the name of a report
I can select a report and click the run button to process the reports.
The text "view" will appear in the View column after i click on the Reports button.
The "view" text will not appear on it's own.
It can be a few clicks until the text appears.
I want to keep clicking the Reports button until the "view" text appears
I think the best solution would be to put this in a while loop somehow.
I have tried but I get the error:
UnboundLocalError: local variable 'element' referenced before assignment
Error Trace is:
Error
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\ReportingPage_TestCase.py", line 81, in test_1_add_matches_report
print reports_page.is_view_link_present_to_view_the_report("Matches")
File "C:\Webdriver\ClearCore\Pages\reports.py", line 321, in is_view_link_present_to_view_the_report
view_element = self.get_element(By.XPATH, '//table[#id="reporting_reports_ct_fields_body"]//td[.//span[text()="%s"]]/following-sibling::td[2]//span[text()="view"]' % report_name)
File "C:\Webdriver\ClearCore\Pages\base.py", line 34, in get_element
return element
UnboundLocalError: local variable 'element' referenced before assignment
The method with the While loop to wait for element to be found is:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
class ReportsPage(BasePage):
def __init__(self, d):
super(ReportsPage, self).__init__(d)
self.datamap_name = UsefulHelperMethods.read_from_file("datamap_name")
self.tool_bar = ToolbarPage(self.driver)
def is_view_link_present_to_view_the_report(self, report_name):
# Params : report_name : Name of the report which to select to view
table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'reporting_reports_ct_fields_body')))
found = False
while not found:
try:
view_element = self.driver.find_element(By.XPATH, '//table[#id="reporting_reports_ct_fields_body"]//td[.//span[text()="%s"]]/following-sibling::td[2]//span[text()="view"]' % report_name)
found = True
except NoSuchElementException:
time.sleep(2)
self.tool_bar.click_reports_button_from_toolbar()
get element defined in my Base Class is:
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
class BasePage(object):
def __init__(self, driver):
self.driver = driver
self.driver.implicitly_wait(120)
# 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
return element
My TestCase class which calls the method is:
from Menus.project_navigator import ProjectNavigatorPage
from Menus.toolbar import ToolbarPage
from Pages.reports import ReportsPage
class ReportingPage_TestCase(BaseTestCase):
def test_1_add_matches_report(self):
tool_bar = ToolBarPage(self.driver)
project_navigator = ProjectNavigatorPage(self.driver)
reports_page = project_navigator.select_projectNavigator_item("Reports")
reports_page.click_add_button_for_reports()
reports_page.select_a_report_checkbox_from_reports_page("Matches")
reports_page.click_run_button_to_run_the_report()
reports_page.click_ok_from_reports_dialog()
tool_bar.click_reports_button_from_toolbar()
print reports_page.is_view_link_present_to_view_the_report("Matches")
How can I keep clicking on the reports button (say every 2 secs) and check if the "view" text is present. Keep trying for about 5 mins.
If at the end of 5 mins it still does not find the view link then to print view text not found, test failed?
I don't want to use time.sleep as that is not effient. The view text could appear in few seconds or a minute depening on how large the report is.
Thanks, help appreciated.
Riaz
The HTML is:
<table id="reporting_reports_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GLKP2TGBFG GLKP2TGBMG" __gwt_subrow="0" __gwt_row="0">
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBHG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7232" style="outline-style:none;" tabindex="0">
<input type="checkbox" tabindex="-1" />
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7233" style="outline-style:none;">
<span class="linkhover" title="Matches" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Matches</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7234" 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="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7235" style="outline-style:none;">
<span class="linkhover block" 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="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7236" style="outline-style:none;">
<span class="" title="Matches report" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Matches report</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7237" 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="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7238" style="outline-style:none;">
<span class="" title="05/11/2015 15:24:47" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">05/11/2015 15:24:47</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7239" style="outline-style:none;">
<span class="block" title="" style="">12</span>
</div>
</td>
<td class="GLKP2TGBEG GLKP2TGBGG GLKP2TGBBH GLKP2TGBNG">
<div __gwt_cell="cell-gwt-uid-7240" style="outline-style:none;">
<span class="" title="INFOSHARE\riaz.ladhani" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">COMPANY\riaz.ladhani</span>
</div>
</td>
</tr>
<tr class="GLKP2TGBEH" __gwt_subrow="0" __gwt_row="1">
</tbody>
element only exists in your try block you must define it outside of a try block. In pseudo code it would look like:
try
{
var element;
}
...
return element; // but does not exist in this scope here
just return it in your try and you're fine, no need for the extra line:
try:
return self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
return None
to continously click the button you can use WebDriverWait (you need to return None if the element was not found in your get_element for this to work).
create a new method that will click and check for the element
def clickTillView:
reportElement.click()
return get_element(self, how, what) != None
then you can wait for clickTillView to be true ergo the element exists
new WebDriverWait(driver, 10000, 1, None).until(clickTillView(), "view was not found in time")
Forgive my Python mistakes, I'm a Java guy :p but this is basically how you can solve it.

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()

get values from table with BeautifulSoup Python

I have a table where I am extracting links and text. Although I can only do one or the other. Any idea how to get both?
Essentially I need to pull the text: "TEXT TO EXTRACT HERE"
for tr in rows:
cols = tr.findAll('td')
count = len(cols)
if len(cols) >1:
third_column = tr.findAll('td')[2].contents
third_column_text = str(third_column)
third_columnSoup = BeautifulSoup(third_column_text)
#issue starts here. How can I get either the text of the elm <td>text here</td> or the href texttext here
for elm in third_columnSoup.findAll("a"):
#print elm.text, third_columnSoup
item = { "code": random.upper(),
"name": elm.text }
items.insert(item )
The HTML Code is the following
<table cellpadding="2" cellspacing="0" id="ListResults">
<tbody>
<tr class="even">
<td colspan="4">sort results: <a href=
"/~/search/af.aspx?some=LOL&Category=All&Page=0&string=&s=a"
rel="nofollow" title=
"sort results in alphabetical order">alphabetical</a> | <strong>rank</strong> ?</td>
</tr>
<tr class="even">
<th>aaa</th>
<th>vvv.</th>
<th>gdfgd</th>
<td></td>
</tr>
<tr class="odd">
<td align="right" width="32">******</td>
<td nowrap width="60"><a href="/aaa.html" title=
"More info and direct link for this meaning...">AAA</a></td>
<td>TEXT TO EXTRACT HERE</td>
<td width="24"></td>
</tr>
<tr class="even">
<td align="right" width="32">******</td>
<td nowrap width="60"><a href="/someLink.html"
title="More info and direct link for this meaning...">AAA</a></td>
<td><a href=
"http://www.fdssfdfdsa.com/aaa">TEXT TO EXTRACT HERE</a></td>
<td width="24">
<a href=
"/~/search/google.aspx?q=lhfjl&f=a&cx=partner-pub-2259206618774155:1712475319&cof=FORID:10&ie=UTF-8"><img border="0"
height="21" src="/~/st/i/find2.gif" width="21"></a>
</td>
</tr>
<tr>
<td width="24"></td>
</tr>
<tr>
<td align="center" colspan="4" style="padding-top:6pt">
<b>Note:</b> We have 5575 other definitions for <strong><a href=
"http://www.ddfsadfsa.com/aaa.html">aaa</a></strong> in our
database</td>
</tr>
</tbody>
</table>
You can just use the text property on a td element:
from bs4 import BeautifulSoup
html = """HERE GOES THE HTML"""
soup = BeautifulSoup(html, 'html.parser')
for tr in soup.find_all('tr'):
columns = tr.find_all('td')
if len(columns) > 2:
print columns[2].text
prints:
TEXT TO EXTRACT HERE
TEXT TO EXTRACT HERE
Hope that helps.
The way to do it is by doing the following:
third_column = tr.find_all('td')[2].contents
third_column_text = str(third_column)
third_columnSoup = BeautifulSoup(third_column_text)
if third_columnSoup:
print third_columnSoup.text