How to count in xpath element

Viewed 66
for i in range(1, 1000):
    
print("/html/body/div[6]/div/div/div[2]/div/div/div['+ i + '/div[3]/button']")

I want it to print as in the example, what should I do?

/html/body/div[6]/div/div/div[2]/div/div/div**[1]**/div[3]/button']

/html/body/div[6]/div/div/div[2]/div/div/div**[2]**/div[3]/button']

/html/body/div[6]/div/div/div[2]/div/div/div**[3]**/div[3]/button']

/html/body/div[6]/div/div/div[2]/div/div/div**[4]**/div[3]/button']

/html/body/div[6]/div/div/div[2]/div/div/div**[5]**/div[3]/button']

/html/body/div[6]/div/div/div[2]/div/div/div**[6]**/div[3]/button']
2 Answers

A few ways to format the print string.

Method 1:

for i in range(1, 10):
    print(f"/html/body/div[6]/div/div/div[2]/div/div/div[{i}]/div[3]/button']")

Method 2:

for i in range(1, 10):
    print("/html/body/div[6]/div/div/div[2]/div/div/div[%s]/div[3]/button']" % (i, ))

Method 3:

for i in range(1, 10):
    print("/html/body/div[6]/div/div/div[2]/div/div/div[{0}]/div[3]/button']".format(i, ))

Reference: https://docs.python.org/3/library/string.html?highlight=string#format-string-syntax

Related