from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Set up the WebDriver using WebDriver Manager
driver = webdriver.Chrome(ChromeDriverManager().install())

# Open the target page
driver.get('URL_OF_YOUR_TARGET_PAGE')

# Wait for the page to load completely
time.sleep(5)  # Adjust this delay as necessary

# Click the button to open the symbol list
spot_button = driver.find_element(By.XPATH, "//button[div[contains(text(), 'Spot')]]")
spot_button.click()

# Wait for the symbol list to be available
time.sleep(2)  # Adjust this delay as necessary

# Click the first element in the symbol list
symbol_list_first_element = driver.find_element(By.CSS_SELECTOR, "[name='symbolList'] > div")
symbol_list_first_element.click()

# Find the input element and click it
search_input = driver.find_element(By.CSS_SELECTOR, "input[placeholder='Search for Symbol (Max: 5)']")
search_input.click()

# Clear the input field and type 'USDT'
search_input.clear()
search_input.send_keys('USDT')

# Trigger the input and change events by pressing Enter
search_input.send_keys(Keys.RETURN)

# Wait for the results to update
time.sleep(2)  # Adjust this delay as necessary

# Continue with the rest of the script
cbIds = []
cbIds.append(driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").find_element(By.XPATH, "..").find_element(By.XPATH, "following-sibling::li").get_attribute('id'))
cbIds.append(driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").find_element(By.XPATH, "..").find_element(By.XPATH, "following-sibling::li[2]").get_attribute('id'))
cbIds.append(driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").find_element(By.XPATH, "..").find_element(By.XPATH, "following-sibling::li[3]").get_attribute('id'))
cbIds.append(driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").find_element(By.XPATH, "..").find_element(By.XPATH, "following-sibling::li[4]").get_attribute('id'))
cbIds.append(driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").find_element(By.XPATH, "..").find_element(By.XPATH, "following-sibling::li[5]").get_attribute('id'))

for i in range(1, 6):
    time.sleep(0.1 * i)
    driver.find_element(By.CSS_SELECTOR, ".bn-sdd-list li.bn-sdd-option-item input[type='checkbox']:checked").find_element(By.XPATH, "..").click()

for i, id in enumerate(cbIds):
    time.sleep(0.1 * i + 0.6)
    driver.find_element(By.ID, id).click()

time.sleep(1.5)
driver.find_element(By.ID, cbIds[0]).find_element(By.XPATH, "../..").find_element(By.CSS_SELECTOR, "button[type='submit']").click()

time.sleep(5)
driver.find_element(By.XPATH, "//button[contains(text(), 'Download') and @type='submit']").click()

# Implement the loop to check if the drawer is closed and restart the process
while True:
    time.sleep(0.2)
    if driver.find_element(By.CSS_SELECTOR, ".drawer-children-container").find_element(By.XPATH, "..").value_of_css_property('display') == 'none':
        dlData()
        break

# Close the WebDriver after finishing the task
driver.quit()
