Riaz Ladhani
Riaz Ladhani

Reputation: 4072

Selenium Python I can iterate over the html table how do I print the values from column 3

I have a HTML table with some rows and columns. I can iterated over the rows and print out all of the column values. I would like to print the values from column 3. How do I do this?

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 implementation 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:
        # Get the columns (all the column 2)
        cols = row.find_elements(By.TAG_NAME, "td")  # note: index start from 0, 1 is col 2
        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")

I have tried:

print col[2].text

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 104, in is_results_displayed_in_data_browser
    print col[2].text
TypeError: 'WebElement' object does not support indexing

I have also tried:

for col in cols:
print cols[2].text

I get the error:

    Traceback (most recent call last):
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Base\BaseTestCase.py", line 174, in tearDownClass
    cls.login_page.click_logout()
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\login.py", line 129, in click_logout
    self.click_yes_from_confirm_dialog_to_confirm()
  File "E:\test_runners 2 edit project\selenium_regression_test_5_1_1\Pages\base.py", line 106, in click_yes_from_confirm_dialog_to_confirm
    yes_button_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'message_dialog_b_yes')))
  File "E:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message: 

Thanks, Riaz

Upvotes: 3

Views: 4184

Answers (4)

Karlo Abapo
Karlo Abapo

Reputation: 41

You may use CSS Selectors to find the nodes under the td. Use td:nth-child() syntax.

 table_id = self.driver.find_element(By.ID,'tableid')

 rows = table_id.find_elements(By.TAG_NAME, "tr")

 for row in rows:
 cols = row.find_elements_by_css_selector('td:nth-child(2)')
 for col in cols:
     print('this is {}'.format(col.text))

Upvotes: 0

saurabh baid
saurabh baid

Reputation: 1877

I have tried:

print col[2].text

Try This

if len(cols)>2:
    cols[2].text

Upvotes: 0

Saurabh Gaur
Saurabh Gaur

Reputation: 23845

'WebElement' object does not support indexing

Actually col is single WebElement while you'r assumming list, so It should be

print cols[2].text

If i do print cols[2].text I get IndexError: list index out of range

You need to check cols list length before passing index to overcome this error as below :-

if len(cols)  >= 3 :
  print cols[2].text

Upvotes: 2

Piyush
Piyush

Reputation: 521

Here I will give you working code in which extract all td tag's value.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Chrome('./chromedriver')

#Open the page
driver.get('file:///.../a.html')

ele = driver.find_elements_by_xpath("//table[@id='search_data_browser_ct_data_browser']/tbody/tr")
for e in ele:
    for td in e.find_elements_by_xpath(".//td"):
        print td.text

Upvotes: 2

Related Questions