Home > database >  Xpath string incrementable with selenium and Python
Xpath string incrementable with selenium and Python

Time:02-28

I am having problems when making an XPATH chain incrementable with a for loop, it is returning errors and I don't know how to solve the problem.

for n_dato in range(2,101):

        name = driver.find_element(By.XPATH,'//*[@id="onu_configured_list"]/table/tbody/tr['   n_dato   ']/td[3]').text;

        print(name)

CodePudding user response:

n_dato is an integer. While concatenating you should type cast it to the string value.

You can try like below ways:

1: Type casting

'//*[@id="onu_configured_list"]/table/tbody/tr[' str(n_dato) ']/td[3]'

2: Using format()

'//*[@id="onu_configured_list"]/table/tbody/tr[{}]/td[3]'.format(n_dato)

3: f string in python

f'//*[@id="onu_configured_list"]/table/tbody/tr[{n_dato}]/td[3]'

CodePudding user response:

I managed to fix the problem using Format this way you can make the XPATH incrementable

for n_dato in range(1,101):
        nombre = driver.find_element(By.XPATH,
            '/html/body/div/div[2]/div/div/table/tbody/tr[{}]/td[3]'.format(n_dato)).text

        print(nombre)
  • Related