2

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.

4
  • 1
    You could maybe use get by XPath instead?
    – Jacques
    CommentedJul 9, 2020 at 18:42
  • 1
    @MichaelMü Update the question with the relevant HTMLCommentedJul 10, 2020 at 4:42
  • 1
    @Jacques: Your right, using XPath was the easier way to go. Thanks for your help! PS, this is my solution: elements = driver.find_elements_by_xpath('//*[contains(@style,"'+attr1+'") or contains(@style,"'+attr2+'")]')CommentedJul 10, 2020 at 13:33
  • @DebanjanB: Thanks, I've already figured it out. However, I really appreciate your interest regarding my issue!CommentedJul 10, 2020 at 13:36

2 Answers 2

1

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

    1

    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.

    1
    • Thanks alot for your contribution! Although I've switched to xpath, I might use your approach for future projects / if I notice that my xpath solution returns wrong results.CommentedJul 10, 2020 at 13:44

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.