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

6
  • is there any error occurred? Commented Sep 26, 2016 at 9:00
  • Your error is in this code block and not int the code which you have mentioned above. yes_button_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'message_dialog_b_yes'))) Commented Sep 26, 2016 at 9:55
  • I think you should do "print cols[2].text" Commented Sep 26, 2016 at 9:56
  • If i do print cols[2].text I get IndexError: list index out of range Commented Sep 26, 2016 at 12:37
  • If i do print cols[0].text it prints out all the ID values from the very first column. Strange cols[2] does not work Commented Sep 26, 2016 at 12:54

4 Answers 4

2

'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
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me. No index out of range anymore. All name values are printed now. Thank you for your help.
2

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

1 Comment

Thanks, I have a working code to print out all column values. I would like to print out all values from column 2. How do i print out values from column 2 only using it's index?
0

I have tried:

print col[2].text

Try This

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

6 Comments

print cols[2].text gives me the error IndexError: list index out of range
If i do print cols[0].text it prints out all the ID values from the very first column. Strange cols[2] does not work.
Ah it is printing the values from cols[2] now. But at the end of the iteration it says IndexError: list index out of range
That is because some your row does not have these column. You only have multiple columns in first row. I think you can easily put the logic to handle that.
I may be able to give you a solution specific to this scenario but a long term solution will based on your actual data and not just the test data.
|
0

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

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.