-2

Would someone be able to tell me why my function (delete_loop) only runs once before exiting? I want it to run 200 times in a row before continuing the code again.   

Thanks

def delete_loop(count):
    if count == 0:
        return
    # Click the dropdown
    dropdown_button = driver.find_element(By.CSS_SELECTOR, '.dropdown > .btn')
    try:
        dropdown_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

    # Click the delete
    delete_button = driver.find_element(By.CSS_SELECTOR, 'a.btn-delete')
    try:                
        delete_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

    # Click the ok
    ok_button = driver.find_element(By.CSS_SELECTOR, 'button#alertify-ok')
    try:
        ok_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass
    
    # Call the function again with decremented count
    delete_loop(count - 1)

# Continuous loop
while True:
    try:
        # Load the ASINs
        input_asins(driver)

        time.sleep(60)

        # Click the download
        download_button = driver.find_element(By.CSS_SELECTOR, 'button.zg-btn-white')
        try:
            download_button.click()
            time.sleep(5) # Wait for 5 seconds to allow the file to download
        except ElementClickInterceptedException:
            pass
        except ElementNotInteractableException:
            pass

        delete_loop(200)

    except Exception as e:
        time.sleep(10)
        input_asins(driver)

     
# Close the Selenium driver
driver.quit()

Full Code

import time
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import ElementNotInteractableException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import csv
from selenium.common.exceptions import TimeoutException


# Set up the Selenium driver
driver = webdriver.Chrome()

options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications') # Disable notifications
driver = webdriver.Chrome(options=options)

# Wait for 2 minutes before starting
time.sleep(60)

driver.get("https://my.zonguru.com/#!/sales-spy")

def input_asins(driver):
    # Load the ASINs from the CSV file
    asins = []
    with open('asins.csv', 'r', encoding='utf-8-sig') as f:
        reader = csv.reader(f)
        for row in reader:
            asins.extend(row)

    # Input the ASINs
    asins_to_input = asins[:200]
    asins = asins[200:]
    asin_input = driver.find_element(By.CSS_SELECTOR, 'input.asins-counter__input')
    asin_input.clear()
    asin_input.send_keys(','.join(asins_to_input))

    # Remove the ASINs that have been inputted
    with open('asins.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows([asins])

    # Select the 'AU' option
    country_select = driver.find_element(By.XPATH, '//*[@id="private-content"]/div/div/div/zg-product-tracker/div/div/div/div[2]/div/form/div[3]/div/select')
    try:
        country_select.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass
    au_option = driver.find_element(By.CSS_SELECTOR, 'option[value="AU"]')
    try:
        au_option.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

    # Click the submit button
    submit_button = driver.find_element(By.CSS_SELECTOR, 'button.zg-btn-green')
    try:
        submit_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

def delete_loop(count):
    if count == 0:
        return
    # Click the dropdown
    dropdown_button = driver.find_element(By.CSS_SELECTOR, '.dropdown > .btn')
    try:
        dropdown_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

    # Click the delete
    delete_button = driver.find_element(By.CSS_SELECTOR, 'a.btn-delete')
    try:                
        delete_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass

    # Click the ok
    ok_button = driver.find_element(By.CSS_SELECTOR, 'button#alertify-ok')
    try:
        ok_button.click()
    except ElementClickInterceptedException:
        pass
    except ElementNotInteractableException:
        pass
    
    # Call the function again with decremented count
    delete_loop(count - 1)

# Continuous loop
while True:
    try:
        # Load the ASINs
        input_asins(driver)

        time.sleep(60)

        # Click the download
        download_button = driver.find_element(By.CSS_SELECTOR, 'button.zg-btn-white')
        try:
            download_button.click()
            time.sleep(5) # Wait for 5 seconds to allow the file to download
        except ElementClickInterceptedException:
            pass
        except ElementNotInteractableException:
            pass

        delete_loop(200)

    except Exception as e:
        time.sleep(10)
        input_asins(driver)

     
# Close the Selenium driver
driver.quit()
Brenduan
  • 9
  • 5
  • 2
    What is happening that makes you think the function only runs once? – John Gordon Feb 28 '23 at 04:27
  • This code is apart of a selenium webdriver that open a chrome tab, the function is meant to delete products from a website continuously, however this only run's once before running the 'input_asins' – Brenduan Feb 28 '23 at 04:32
  • 1
    In the bottom `except Exception as e` block, print the exception. I suspect some other exception is happening. – John Gordon Feb 28 '23 at 04:49

2 Answers2

0

You can start your function like this:

def delete_loop(count):
    for i in range(count):
        # Click the dropdown
        dropdown_button = driver.find_element(By.CSS_SELECTOR, '.dropdown > .btn')

Then you need to indent the following codes once to the right. Also, remove this call: delete_loop(count - 1)

-2

shouldn't it be like this:

 delete_loop(count -= 1)

and instead of checking if, I would do a while loop