简体   繁体   中英

How to get data from a specific row within an html table by using selenium webdriver in python

I am completely new to python and selenium, so please forgive all the mistakes and erroneous assumptions.

I try to get a certain value from an html table that is always in column 4 while the row may change depending on the search result. The html code is built in a way that each tr has a th scope="row" as a child and then also 4 tds as children on the same level. Now what my code needs to do is: find the tr where th scope="row"="MyString" and then go into column 4, so td[4], and then output the th of that row again and the value in td[4].

Here is the html code

<div id="ctl00_ContentPlaceHolder1_dvDetailsTableAmbulantMisc">

                <table class="details-table ambulant misc nophone" role="grid">
                    <thead>
                        <tr role="row">
                            <th scope="col">Bezeichnung</th><th role="gridcell"></th>
                            <th scope="col">Kategorie</th><th role="gridcell"></th>
                            <th scope="col">Preis</th>
                        </tr>
                    </thead>
                    <tbody>

                <tr>
                    <th scope="row">Ganzwaschung</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl0_tdPreis1">24,39</td>

                </tr>

                <tr>
                    <th scope="row">Teilwaschung</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl1_tdPreis1">13,05</td>

                </tr>

                <tr>
                    <th scope="row">Ausscheidungen</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl2_tdPreis1">5,95</td>

                </tr>

                <tr>
                    <th scope="row">Selbständige Nahrungsaufnahme</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl3_tdPreis1">5,95</td>

                </tr>

                <tr>
                    <th scope="row">Hilfe bei der Nahrungsaufnahme</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl4_tdPreis1">14,89</td>

                </tr>

                <tr>
                    <th scope="row">Sondenernährung bei implantierter Magensonde (PEG)</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl5_tdPreis1">5,95</td>

                </tr>

                <tr>
                    <th scope="row">Lagern/Betten</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl6_tdPreis1">5,95</td>

                </tr>

                <tr>
                    <th scope="row">Mobilisation (Mindeseinsatzdauer 15 Minuten)</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl7_tdPreis1">10,71</td>

                </tr>

                <tr>
                    <th scope="row">Waschen und Pflegen der Wäsche und Kleidung</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl8_tdPreis1">20,61</td>

                </tr>

                <tr>
                    <th scope="row">Große Grundpflege mit Lagern/Betten und selbständiger Nahrungsaufnahme</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl9_tdPreis1">36,24</td>

                </tr>

                <tr>
                    <th scope="row">Große Grundpflege</th><td></td>
                    <td>Körperbezogene Pflegemaßnahmen</td><td></td>
                    <td id="ctl00_ContentPlaceHolder1_lvDetailsTableAmbulantMisc_ctrl10_tdPreis1">26,74</td>

How would I go about doing this? If you want I can also share my code that shows how to get to the table.

You could do something like this...

rows = driver.find_elements_by_css_selector('.details-table tbody>tr')
row = next(filter(lambda x: x.find_element_by_xpath('th[1]').text == 'MyString', rows))
value = row.find_element_by_xpath('td[4]').text

rows gets you all the rows.
row does a filter and find the first th foreach row that matches MyString then returns that row (so we are still at the tr level).
value then does a find element on that tr by finding td[4] and gets the text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM