Python + Selenium to read html table

Asked by At

I am trying to get the data of out a table that has no Ids I seem to not be able to search on when using Python and Selenium. Below is a snippet of the HTML, I am trying to get the Name, there are other rows after name, but Im just showing the name here. Any ideas how I can loop through all these entries and grab their values.

Ive tried some examples of searching for the Div class but could not get it to work correctly. Below is the raw HTML that I am trying to search through.

<div class="block-content-body clear-block">
    <div class="inline-block" style="border-top:2px solid #50afb1; padding:0px;">
        <div class="table-block standard no-plumb-line">
            <table cellpadding="0" cellspacing="0">
            <td id="" style="padding-left:4px;padding-bottom:2px;height:25px;vertical-align:middle">             
        <td id="" class="text-center" style="padding-left:4px;padding-bottom:2px;height:25px;vertical-align:middle">


I want to get the Name field and the value David. There are other fields under like Address line 1 and 2 etc. I want to get all these.

My code is as follows:

elements = driver.find_elements_by_xpath("//div[@class='table-block standard no-plumb-line']")

    for num in (1,elements.length):

        text1 = driver.find_element_by_xpath("//*[@class='table-block standard no-plumb-line']/div["+num+"]/div[1]").text
        text2 = driver.find_element_by_xpath("//*[@class='table-block standard no-plumb-line']/div["+num+"]/div[2]").text        

1 Answers

supputuri On Best Solutions

Here is the script with the required xpath that will give Name : Value pair.

rows = driver.find_elements_by_xpath("//div[@class='table-block standard no-plumb-line']/table//tr")

for rowNum in range(len(rows)):
    nameCols = driver.find_elements_by_xpath("//div[@class='table-block standard no-plumb-line']/table//tr[" + str(rowNum+1) + "]/td[descendant::div[not(strong)]]")
    for col in nameCols:
        name = col.text
        value = col.find_element_by_xpath("./following-sibling::td//strong").text
        print (name + " : " + value)