Python Selenium: Find element by CSS Properties

Viewed 1112

Hi Community! This is my first question, I hope that I do not violate any rules. Any help is highly appreciated - thank you all in advance!

My problem can be described as follows: I am trying to scrape content between two consecutive headers. Finding the first header is fairly easy, as I know the exact name of it. However, I have no information on the second, consecutive header besides that it must have the same style CSS properties.

My approach is to find the next element having the same CSS properties, for example the same "font-size", etc. Yet, it does not run properly.

This is a simplified, brief example of what I'm trying to achieve. Unfortunately, my code will always provide me empty results:

##Get the attribute of the first element (header), which I found already##
attr1 = str(element.value_of_css_property("font-size"))

##Get list of items with the same css-property so that I can find the "consecutive element"
elements = driver.find_elements_by_css_selector("*[font-size='"+attr1 +"']")

As I am new to programming, please forgive me beginner-misstakes.

2 Answers

When searching by css properties, I usually do seomthing like the following:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

css_selection = '[font-size="%s"]' % attribute
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, css_selection)))
element = driver.find_element_by_css_selector(css_selection)

You don't need the asterisk and I think you're using too many quotes. It's easier to format the string than to concatenation. Not sure if this solves your exact problem since I don't know the full details of the project, but works whenever I use it.

You might want to use locate by xpath instead. It is easier to target elements rather than using css styles or elements that don't have id's or classes.

You can read more here

Related