random
random

Reputation: 33

selenium css_selector struggle

i got a css-selector that reads

#block-a4e7-et-lists-a4e7-et-lists-content > div > div > div.row > div.col-md-9.col-sm-12.col-lg-9.col-xs-12 > div > div.a4e7-fw-pagination > div:nth-child(5)

on the webpage

http://www.ecotechnology.at/de/content/ausbildung-weiterbildung-und-gr%C3%BCne-stellenangebote?etsv=karriere_at

but even after long struggle i do not manage to access it with selenium's driver.find_element_by_css_selector() function in python.

could you help me with this?

best anda

Upvotes: 0

Views: 167

Answers (1)

alecxe
alecxe

Reputation: 473753

The selector itself is too fragile because:

  • it heavily relies on direct parent-child relationship and hence the structure of the page
  • it uses layout-oriented classes like col-xs-12 that are usually too broad and have a higher chance to be changed

Instead, I'd use the following CSS selector:

.content .a4e7-fw-pagination > div:nth-child(5)

You might also be experiencing a timing problem - the element is not yet present when you search for it. The common way to tackle problems like this is an Explicit Wait. To be specific, in your case, you might have something like:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".content .a4e7-fw-pagination > div:nth-child(5)")))

Upvotes: 1

Related Questions