• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

101
Vistas
How to implement Expected Conditions with Javascript calls in Selenium

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%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 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?

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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()
about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda