My Python Selenium code below 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,-43.509500,10.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 it works, I want to know how to use this syntax within the framework of expected conditions- that is, instead of having to tell Selenium to sleep for ten seconds, how would I use
wait.until()
with the Javascript so I don't need to explicitly set the time to wait for ten seconds- after all, what if it took less than ten seconds? Any idea how I might do this?
CodePudding user response:
You can pass the webelement returned through execute_script() as an argument to the expected_conditions of element_to_be_clickable() and invoke the click as follows:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable(driver.execute_script("return 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()