selenium 如何在每次循环运行时将索引加1?

cs7cruho  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(130)

我想在每次循环运行时将index递增1,但我不知道如何实现。
这是我现在的代码:

categoryindex = categorylist[1]

while True:
    try:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[child::span[text()='{oddsnumber}'] and span[text()='{oddstype}']]"))).click()
    except:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()

我希望它从索引[1]开始,直到找到请求的元素。我尝试使用+ increment,但我不确定它如何与索引一起工作。

lnlaulya

lnlaulya1#

你可以使用for循环。也可以使用break语句。这将在找到元素时中断循环,如果没有找到,它将递增索引并在下一次迭代中再次检查。

for i in range(1, len(categorylist)):
    categoryindex = categorylist[i]
    try:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[child::span[text()='{oddsnumber}'] and span[text()='{oddstype}']]"))).click()
      
    except:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()
disho6za

disho6za2#

您可以使用next()函数来完成此操作,而无需使用任何索引。首先,您必须从您的可迭代对象创建一个迭代器。可迭代对象的值是您称为categorylist的列表。要从它创建一个迭代器,只需使用iter(categorylist)
然后你可以通过调用next()函数来迭代你的列表。每次你调用它的时候,它会从第一个项目到最后一个项目返回列表项目。
示例:

my_list = ['a', 'b', 'c', 'd']
my_list_iterator = iter(my_list)
print(next(my_list_iterator)) #optput:'a'
print(next(my_list_iterator)) #optput:'b'
print(next(my_list_iterator)) #optput:'c'
print(next(my_list_iterator)) #optput:'d'

如果再次调用next,则将引发异常StopIteration
有两种方法可以避免该错误:

# Way 1: pass second parameter to next() function to be returned if all list items iterated
my_list = ['a', 'b', 'c', 'd']
my_list_iterator = iter(my_list)
MY_DEFAULT = 'my_default'
print(next(my_list_iterator, MY_DEFAULT)) #optput:'a'
print(next(my_list_iterator, MY_DEFAULT)) #optput:'b'
print(next(my_list_iterator, MY_DEFAULT)) #optput:'c'
print(next(my_list_iterator, MY_DEFAULT)) #optput:'d'
print(next(my_list_iterator, MY_DEFAULT)) #optput:'my_default'
...
print(next(my_list_iterator, MY_DEFAULT)) #optput:'my_default'

# Way 2: use the funtion next() as many as the list size:
for i in range(len(my_list)):
    print(next(my_list_iterator))
# outputs: prints 'a', 'b', 'c', 'd' each one in new line

如何在代码中使用它?

categorylist_iterator = iter(categorylist)

next(categorylist_iterator) # iterate the 0th index to start from index 1 in the loop
while True:
    categoryindex = next(categorylist_iterator)
    try:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[child::span[text()='{oddsnumber}'] and span[text()='{oddstype}']]"))).click()
    except:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()

但是如何避免代码中的StopIteration错误。如果categorylist上的迭代结束,则会引发错误!

DEFAULT_INDEX = 'A value which not exists in the list categorylist. for example it can be a random token'
categorylist_iterator = iter(categorylist)

next(categorylist_iterator) # iterate the 0th index to start from index 1 in the loop
while True:
    categoryindex = next(categorylist_iterator, DEFAULT_INDEX)
    if categoryindex == DEFAULT_INDEX:
        break
    try:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[child::span[text()='{oddsnumber}'] and span[text()='{oddstype}']]"))).click()
    except:
        ignored_exceptions = (NoSuchElementException, StaleElementReferenceException,)
        WebDriverWait(driver, 20, ignored_exceptions=NoSuchElementException).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()

相关问题