Monic Annadurai
Monic Annadurai

Reputation: 47

How to automatically increase the xpath range in python and selenium

How to set automatically increase from [@id='myGridView_0'] to [@id='myGridView_1'] and [@id='myGridView_2'] and so on ?

period=[]
for i in range(2,rows+1):
    for j in range(1,cols+1,2):
        period.append(driver.find_element_by_xpath("//*[@id='myGridView_0']/tbody/tr[" + str(i) + "]/td[" + str(
            j) + "]").text)

Upvotes: 0

Views: 251

Answers (1)

Guy
Guy

Reputation: 50939

You can do it the same way you are using variable in tr[]/td[]

period = []
for k in range(myGridView number):
    for i in range(2,rows+1):
        for j in range(1,cols+1,2):
            period.append(driver.find_element_by_xpath(f"//*[@id='myGridView_{k}']/tbody/tr[{i}]/td[{j}]").text)

You can remove the outer loop if myGridView number has correlation to i or j and use them instead.

Upvotes: 1

Related Questions