Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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
        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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading