typeerror: list indices must be integers or slices not str: trying to loop through table elements in selenium

I have a piece of code where I’m trying to loop through a table in selenium and store the text in a list to check if it matches the user input:

 def choose_class(self):
    crsNums = driver.find_elements(By.XPATH, "//*[@id='table1']/tbody/tr/td")
    crsList = []
    for i in crsNums:
        classes = i.text

        for j in crsList:
            if int(crsList[j]) == crn:
                chsnclass = crsList[j]

I get an an error saying that the index must be an int, when the I get the user input do a conversion to make sure it’s an int: crn = int(input("Enter the CRN (course registration #): ")) but it still doesn’t work?

>Solution :

Because j, which is classes ,which is i.text is not integer probably. if you are sure it is a string that contains numbers, then crsList[int(j)] could solve the problem

Leave a Reply