1

I have this structure :

<table>
<tbody>
    <tr id="1_2011_11_11_07_45_00" class="on">
    </tr>
    <tr id="1_2011_11_11_09_25_00">
    </tr>
    <tr id="1_2011_11_11_11_05_00">
    </tr>
    <tr id="1_2011_11_11_14_50_00">
    </tr>
    <tr id="1_2011_11_11_16_00_00">
    </tr>
    <tr id="1_2011_11_11_18_10_00">
    </tr>
    <tr id="1_2011_11_11_21_30_00">
    </tr>
</tbody>

and I would like to count the number of lines that are in the table. I am using Python for the script. The xpath of the table is :

xpath=/html/body/form/div[3]/div/div/div[2]/div/div/table

Anyone could help me ?

3 Answers 3

3

Can also be done via get_xpath_count. for ex. Number_of_row = $browser.get_xpath_count("/tbody/tr")

I have not checked the above code but I think it will work

Sign up to request clarification or add additional context in comments.

2 Comments

Great Thank you ! however in my page 2 tables have almost the same Xpath, the other one has : xpath=/html/body/form/div[3]/div/div/div[2]/div[2]/div/table and it takes the count of both,i don't know why
Its might be because the xpath_count of "/tbody" be 2. Hence taking the "tr" count for both the "/tbody". Try giving the absolute xpath for the table you want to count
0
s = """<table>
<tbody>
    <tr id="1_2011_11_11_07_45_00" class="on">
    </tr>
    <tr id="1_2011_11_11_09_25_00">
    </tr>
    <tr id="1_2011_11_11_11_05_00">
    </tr>
    <tr id="1_2011_11_11_14_50_00">
    </tr>
    <tr id="1_2011_11_11_16_00_00">
    </tr>
    <tr id="1_2011_11_11_18_10_00">
    </tr>
    <tr id="1_2011_11_11_21_30_00">
    </tr>
</tbody>"""
import re
len(re.findall('\tr',s))

Comments

0

Xpath contains a count(<node-set expr>) function.

Simplifying your example, if your table were the only table in the html source, then the the xpath expression count(//table//tr) would return the number 7.

Comments

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.