-1
<table border="0" cellpadding="0" cellspacing="0" class="A490847cc28c94895bcf96e98abdb2b32209xB">
                  <tbody>
                   <tr>
                    <td style="vertical-align:top">
                     <table border="0" cellpadding="0" cellspacing="0" class="A490847cc28c94895bcf96e98abdb2b32206" cols="8" style="border-collapse:collapse;">
                      <tbody>
                       <tr height="0">
                        <td style="WIDTH:56.91mm;min-width:56.91mm">
                        </td>

                       </tr>
                       <tr valign="top">

                        <td class="A490847cc28c94895bcf96e98abdb2b32126c Pe5cd046a24fa44ef80e79727141463c6_1_r7">
                         <div class="A490847cc28c94895bcf96e98abdb2b32126">
                          Total Tutorials
                         </div>
                        </td>


                        <td class="A490847cc28c94895bcf96e98abdb2b32144c Pe5cd046a24fa44ef80e79727141463c6_1_r7 Pe5cd046a24fa44ef80e79727141463c6_1_r6">
                         <div class="A490847cc28c94895bcf96e98abdb2b32144">
                          <div style="WIDTH:19.98mm;">
                           <div class="A490847cc28c94895bcf96e98abdb2b32141">
                            <span class="A490847cc28c94895bcf96e98abdb2b32140">
                             Attendance
                            </span>
                           </div>
                           <div class="A490847cc28c94895bcf96e98abdb2b32143">
                            <span class="A490847cc28c94895bcf96e98abdb2b32142">
                             %
                            </span>
                           </div>
                          </div>
                         </div>
                        </td>
                       </tr>
                       <tr valign="top">
                        <td class="A490847cc28c94895bcf96e98abdb2b32149cl Pe5cd046a24fa44ef80e79727141463c6_1_r5" style="HEIGHT:6.35mm;">
                         <div class="A490847cc28c94895bcf96e98abdb2b32149">
                          CSFf
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32153cr">
                         <div class="A490847cc28c94895bcf96e98abdb2b32153">
                          9
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32157cr">
                         <div class="A490847cc28c94895bcf96e98abdb2b32157">
                          7
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32161cl">
                         <div class="A490847cc28c94895bcf96e98abdb2b32161">
                          0
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32165cl">
                         <div class="A490847cc28c94895bcf96e98abdb2b32165">
                          0
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32169cr">
                         <div class="A490847cc28c94895bcf96e98abdb2b32169">
                          4.0000
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32173cr">
                         <div class="A490847cc28c94895bcf96e98abdb2b32173">
                          4.0000
                         </div>
                        </td>
                        <td class="A490847cc28c94895bcf96e98abdb2b32177cr Pe5cd046a24fa44ef80e79727141463c6_1_r6">
                         <div class="A490847cc28c94895bcf96e98abdb2b32177">
                          84.62
                         </div>
                        </td>

I am working with python selenium web driver.I want the loop through the table for the text inside the td inside tr tag like this
But problem here is the class name is always changing and xpath and css_selector is also changing.example xpath will be like this .//*[@id='P825048fc6b084257a601fde4805c8c33_1_oReportCell']/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td[8]/div
But the id is changing always.so couldn't apply driver.find_element_by_id().I think regular expressions or BeautifulSoup can be used to solve this.I am beginner to regex.So is there any way this could be solved?

16
  • Your html is not complete enough to determine how to do this. Could you instead post the whole table, with only some rows? So <table><tbody><tr><td><table><tbody><tr> <<< that whole table, with only the first two rows on each table included. Commented Aug 31, 2016 at 8:28
  • Your xpath already looks way over complicated. What page are you scraping and what do you want? Commented Aug 31, 2016 at 9:03
  • @PadraicCunningham hello!that is aspx site in which there is a table and I want to get the data from the table Commented Aug 31, 2016 at 9:06
  • 1
    Try using "//*[contains(@id,'oReportCell')]", I presume only the starting numbers change? Commented Aug 31, 2016 at 10:27
  • 1
    It worked.I added to your answer //*[contains(@id,'oReportCell')]/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td[1] Commented Aug 31, 2016 at 13:02

2 Answers 2

1

The parent of table id has kind of unique in which every time the number changes but the ReportCell is appended to it so using that we can find its children like this

//*[contains(@id,'oReportCell')]/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td[1]
Sign up to request clarification or add additional context in comments.

Comments

0

You might be able to use this xpath, not sure if it's 100% correct:

//div[text() = 'CSFf']/ancestor::tr/td[not(descendant::div[text() = 'CSFf'])]/div

To make it variable:

public String getXpathForTableThing(String searchString) {

    return "//div[text() = '"+searchString+"']/ancestor::tr/td[not(descendant::div[text() = '"+searchString+"'])]/div";
}

I'm not experienced with python but I'm sure you get the idea and can transform this to a python function.

9 Comments

I'll try this and let you know
working.But not accurately.This selects the div's which shouldn't be included
but what if in the place of CSFf there is another text.I have to change the text to the desired string But can there any way to get this without including the text CSFf.?
What I do when I have a badly made html table like this is create a function for my xpath where I supply the text used to find the rows.
what if I want to get them what ever is there in that
|

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.