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

Time:01-12

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
        crsList.append(classes)
        print(crsList)

        for j in crsList:
            if int(crsList[j]) == crn:
                chsnclass = crsList[j]
                webdriver.ActionChains(driver).double_click(chsnclass).key_down(Keys.META).send_keys('c').perform()

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?

CodePudding user response:

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

  • Related