Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
¿Hay alguna manera de obtener una lista de elementos de una llamada de Javascript en Python usando selenio?

Así que recientemente descubrí que puedes ejecutar JavaScript en navegadores usando Selenium en Python. Tengo una función que devuelve una lista de elementos que se ve así:

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

Al ejecutar esto en la consola del navegador, obtiene una lista de todos los href -s de los nietos de cada elemento que tiene una class de ejemplo.

¿Es posible hacer algo como esto usando llamadas Javascript directas o funciones integradas de Selenium?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para extraer todos los valores href a python, puede intentar lo siguiente:

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

O, si solo desea llamar a un código js en el navegador en una llamada de una sola línea:

 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 Report

0

para obtener el href children's en Selenium nativo, equivalent a su código, sería algo como esto:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!