2

This is the html

<table id="dataLstSubCat" cellspacing="0" style="border-collapse:collapse;">
    <tbody><tr>
        <td style="font-weight:normal;font-style:normal;text-decoration:none;white-space:nowrap;">
                        <a onclick="ShowHideProduct();" id="dataLstSubCat_LnkBtnSubCat_0" href="javascript:__doPostBack('dataLstSubCat$ctl00$LnkBtnSubCat','')">Primers</a>
                      </td><td style="font-weight:normal;font-style:normal;text-decoration:none;white-space:nowrap;">
                        <a onclick="ShowHideProduct();" id="dataLstSubCat_LnkBtnSubCat_1" href="javascript:__doPostBack('dataLstSubCat$ctl01$LnkBtnSubCat','')">Intermediates</a>
                      </td><td style="font-weight:normal;font-style:normal;text-decoration:none;white-space:nowrap;">
                        <a onclick="ShowHideProduct();" id="dataLstSubCat_LnkBtnSubCat_2" href="javascript:__doPostBack('dataLstSubCat$ctl02$LnkBtnSubCat','')">Finishes</a>
                      </td>
    </tr>
</tbody></table>

Now I want to extract the table data(td) text like I want to extract the text

[Primers,Intermediates,Finishes]

This is what I have tried

new_text=driver.find_element_by_xpath(("//table[@id='dataLstSubCat']/tbody/tr"))
new_text.text

which gives o/p in string and not in list

Primers Intermediates Finishes

Is there any way by which it can be done.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Andre_k
  • 1,680
  • 3
  • 18
  • 41
  • would new_text.text.split(' ') solve the problem? – Nathan May 27 '19 at 09:40
  • @Nathan in case if tr element will have spaces in text it wont work anymore – Robert May 27 '19 at 09:41
  • @Nathan , It would not solve the problem, because **space** would not be the criteria for splitting, for e.g consider the **Dry Primers** is the single word , if i split it by space it would not solve the purpose – Andre_k May 27 '19 at 09:46

2 Answers2

1

One option is to use find_elements_by_xpath and then with for loop add it to the list like:

list = []
new_text=driver.find_elements_by_xpath(("//table[@id='dataLstSubCat']/tbody/tr/td"))
for text in new_text:
   list.append(text.text)
Robert
  • 171
  • 4
  • It is treating it as a single string ['Primers Intermediates Finishes'] and not as a separate list elements. – Andre_k May 27 '19 at 09:47
  • ah I see, probably you need to update xpath to use td elements of table "//table[@id='dataLstSubCat']/tbody/tr/td" – Robert May 27 '19 at 09:51
1

To extract the table data [Primers,Intermediates,Finishes] you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print([my_text_elem.get_attribute("innerHTML") for my_text_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "table#dataLstSubCat>tbody>tr td>a")))])
    
  • Using XPATH:

    print([my_text_elem.get_attribute("innerHTML") for my_text_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@id='dataLstSubCat']/tbody/tr//td/a")))])
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352