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

153
Vistas
Selecting a element by it's css selector in puppeteer

I'm trying to select an image element using its style selector. I wrote the line of code in Python but I'm having problems translating it to JavaScript. Here's my attempt so far. Please note the python code works. It's the js I'm having problems with.

if driver.find_element_by_css_selector("img[style='object-fit: cover;']") is not None:
        download_url = driver.find_element_by_css_selector("img[style='object-fit: cover;']").get_attribute('src')

And here is my js attempt.

let imageArr = []

for(let post of posts) {
    await page.goto(post)
    await page.waitForTimeout(6000)

    if (await page.type("img[style='object-fit: cover;']") !== null) {

        const image = await page.evaluate(() => {
            document.querySelectorAll("img[style='object-fit: cover;']").forEach(img => {
                let imageUrl = img.getAttribute('src');
                imageArr.push(imageUrl)
            })
        })
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you need to use the * CSS attribute selector, from MDN:

[attr*=value] - Represents elements with an attribute name of attr whose value contains at least one occurrence of value within the string.

So for your Javascript code, I believe this will work:

let imageArr = []

for(let post of posts) {
    await page.goto(post)
    await page.waitForTimeout(6000)

    if (await page.type("img[style*='object-fit: cover;']") !== null) {

        const image = await page.evaluate(() => {
            document.querySelectorAll("img[style*='object-fit: cover;']").forEach(img => {
                let imageUrl = img.getAttribute('src');
                imageArr.push(imageUrl)
            })
        })
    }
}

This will select all img elements that have at least one occurrence of object-fit: cover; within the string.

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