Below is my Python Selenium code that downloads a shapefile of Rio de Janeiro.
import time, os
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
options = webdriver.ChromeOptions()
preferences= {"download.default_directory": os.getcwd(), "directory_upgrade": True}
options.add_experimental_option("prefs", preferences)
#options.headless = True
options.add_experimental_option('excludeSwitches', ['enable-logging'])
url = "https://www.data.rio/datasets/limite-bairro/explore?location=-22.900784%2C-43.509500%2C10.83"
# Path of my WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
wait = WebDriverWait(driver, 10)
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ember104"))).click()
time.sleep(10)
driver.execute_script('document.querySelector("#ember50 > div > div > div:nth-child(1) > div.download-panel > div > div:nth-child(8) > hub-download-card").shadowRoot.querySelector("calcite-card > div > calcite-dropdown > calcite-button").click()')
time.sleep(10)
driver.execute_script('document.querySelector("#ember50 > div > div > div:nth-child(1) > div.download-panel > div > div:nth-child(8) > hub-download-card").shadowRoot.querySelector("calcite-card > div > calcite-dropdown > calcite-dropdown-group > calcite-dropdown-item:nth-child(1)").click()')
While this code functions well, I am interested in utilizing the syntax within the framework of expected conditions. Instead of defining a specific wait time of ten seconds using time.sleep(), how can I incorporate
wait.until()
within the Javascript so that the script doesn't need to wait for a fixed amount of time? Is there a way to dynamically determine the time needed to wait instead of setting it to ten seconds?