Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Is there a way to get a list of items from a Javascript call in Python using selenium?

So I have recently found out that you can execute JavaScript in browsers using Selenium in Python. I have a function that returns a list of items that looks like this:

var elements = document.getElementsByClassName('example');
elements = [...elements].map(el => el.children[0].children[0].getAttribute('href').toString());

Executing this in the Browser console gets you a list of all href-s of the grandchildren of every element which has a class of example.

Is it possible to do something like this using either direct Javascript-calls or built-in Selenium functions?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

To extract all the href values to python you could try the following:

elems = driver.find_elements_by_css_selector(".example > *:first-of-type > *:first-of-type[href]")
links = [elem.get_attribute('href') for elem in elems]

Or, if you just want to call a js code on the browser in a single-line call:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("<your_website>") 

driver.execute_script("console.log('myElements', Array.from(document.getElementsByClassName('example')).map(elm => elm.firstElementChild.firstElementChild.getAttribute('href').toString()))")
about 4 years ago · Juan Pablo Isaza Denunciar

0

to get the children's href in native Selenium, equivalent to your code, it'd be something like this :

elems = driver.find_elements_by_xpath("example xpath")
for ele in elems:
    link = ele.find_element_by_xpath(".//child::*").get_attribute('href')
    print(link)
    
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda