【问题标题】:typeerror: list indices must be integers or slices not str: trying to loop through table elements in seleniumtypeerror: list indices must be integers or slice not str: trying to loop through table elements in 硒
【发布时间】:2023-01-12 07:19:48
【问题描述】:

我有一段代码,我试图在 selenium 中遍历一个表并将文本存储在列表中以检查它是否与用户输入匹配:

 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()

我得到一个错误,指出索引必须是一个 int,当我得到用户输入时进行转换以确保它是一个 int:crn = int(input("Enter the CRN (course registration #): ")) 但它仍然不起作用?

【问题讨论】:

  • 始终包括完整的 Traceback - 复制并粘贴它然后将其格式化为代码(选择它并键入 ctrl-k)。

标签: python selenium selenium-webdriver


【解决方案1】:

因为 j,即类,即 i.text 可能不是整数。如果您确定它是一个包含数字的字符串,那么crsList[int(j)] 可以解决问题

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2022-11-16
    • 2018-09-30
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多