Umar Yusuf
Umar Yusuf

Reputation: 984

Python selenium - Access html tags with a table

I have this table:-

<table>
<tbody>
    <tr class="stripe">
        <td colspan="3"/>
    </tr>
    <tr>
        <td style="width: 160px;">Field1:</td>
        <td style="width: 250px;">
        <strong>
        <span id="lblSalesExec">item1</span>
        </strong>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>Field2:</td>
        <td>
        <strong>
        <span id="lblLocation">item2</span>
        </strong>
        </td>
        <td/>
    </tr>
    <tr>
    <th colspan="3">
    <h3 style="color: rgb(255, 255, 255);">Other Details</h3>
    </th>
    </tr>
    <tr>
    <td>Field3:</td>
    <td>
    <span id="lblRef">item3</span>
    </td>
    <td/>
    </tr>
    <tr>
    <td>Field4:</td>
    <td>
    <span id="lblCustomerName">item4</span>
    </td>
    <td/>
    </tr>
    <tr>
    <td>Field5:</td>
    <td>
    <span id="lblCurrentAddress">
    item5-1
    <br/>
    item5-2
    <br/>
    item5-3
    <br/>
    item5-4
    </span>
    </td>
    <td/>
    </tr>
    <tr>
    <td>
    <i class="icon-envelope"/>
    Field6:
    </td>
    <td>
    <input name="txtCustomerEmail" type="text" value="item6" id="txtCustomerEmail" style="width: 250px;"/>
    </td>
    <td/>
    </tr>
    <tr>
    <td>
    <i class="icon-phone"/>
    Field7:
    </td>
    <td>
    <input name="txtCustomerTelNo1" type="text" value="item7" id="txtCustomerTelNo1" style="width: 250px;"/>
    </td>
    <td/>
    </tr>
    <tr>
    <td>
    <i class="icon-phone"/>
    Field8:
    </td>
    <td>
    <input name="txtCustomerTelNo2" type="text" id="txtCustomerTelNo2" style="width: 250px;"/>
    </td>
    <td/>
    </tr>
    <tr>
    <td>
    <i class="icon-phone"/>
    Field9:
    </td>
    <td>
    <input name="txtCustomerTelNo3" type="text" id="txtCustomerTelNo3" style="width: 250px;"/>
    </td>
    <td/>
    </tr>
    <tr>
    <td>Field10:</td>
    <td>
    <span id="lblCurrentVehicle">
    item10
    <br/>
    item10-1
    <br/>
    item10-2
    </span>
    </td>
    <td/>
    </tr>


    <tr>
    <th colspan="3">

    </th>
    </tr>
    <tr>

</tbody>
</table>

How to I use Python selenium to parse a table with lots of varying html tags in its rows? See the attached image below with the expected output table.

enter image description here

This is what I have don so far...

ele = driver.find_element_by_class_name("list")
ele_txt = ele.text
spli = re.split('\n', ele_txt)
# spli1 = re.split(':', ele_txt)

spli

Upvotes: 0

Views: 909

Answers (1)

Xwris Stoixeia
Xwris Stoixeia

Reputation: 1861

First you want find_elementS not find_element. If I understood your goal well, you are aiming to get the text from the span id's; hence this is where I will focus my xPath expression. So with the below you should be able to find all elements which correspond to html span id in a clever way; meaning without specifying any particular value for the specific span id/html tag. Here is the xpath:

//tr[@span]

Now using find_elements you can return a list of elements matching the Xpath (so, all the span ids); then you extract the text from them.

for ele in driver.find_elements_by_xpath("//tr[@span]"):
    print ele.text

Best of luck!

Update after OP's comment:

enter image description here

Think of your html tags as a tree. You start from the top and work your way down to the attribute that you want. So if your html tag is input (and the attribute you want to grab is type='submit') this translates like following in xpath: //htmlTag[@attribute='value'] --> so we will have --> //input[@type='submit'] You can focus on an element if you load you xPath helper for Chrome and right click on an element and select Inspect; then it focuses on what you need to select :)

Upvotes: 1

Related Questions