Home > OS >  How to increment index by 1 every time loop is run?
How to increment index by 1 every time loop is run?

Time:01-22

I want to increment index by 1 every time loop is run, but I don't know how to do it.

This is the code I have right now :

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

I want it to start from index [1] until requested element is found. I tried using increment but I'm not sure how it works with index.

CodePudding user response:

You can use for loop. Also use break statement. This will break the loop when the element is found and if not found, it will increment the index and check again in the next iteration.

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

CodePudding user response:

You can use next() funtion to do so without using any index. first, you must create an iterator from your iterable. the iterble value is the list you called categorylist. to create an iterator from it just use iter(categorylist)
Then you can iterate your list by calling next() funtion on it. it will return list items from first item to last each time you call it.
example:

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'

If you call next one more time an exception will be raised as StopIteration
To avoid the error there is two ways:

# 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

How to use it in your code?

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

But how to avoid StopIteration error in the code. if the iteration on categorylist be ended the error will be raised!

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