0

Trying to get the Finance data from this div. There is no unique identifier for this div. So, I collect all 3-4 divs check if the word FINANSE appears in the text, if it does, then get the inner div text. However, it doesn't seem to work. Any other approach or what am I missing here? Thanks in advance.

link = https://rejestr.io/krs/882875/fortuna-cargo

fin_divs = driver.find_elements_by_css_selector('div.card.mb-4')
for div in fin_divs:
    if 'FINANSE' in div.text:
        finances = div.find_element_by_css_selector('div.card-body').text
    else:
        finances = "Finance Data Not Available"

2 Answers 2

1

You can simplify your code to select exact element instead of looping through list of elements:

finances = driver.find_element_by_xpath('//div[div="Finanse"]/div[@class="card-body"]').text
print(finances)

>>>Kapitał zakładowy
>>>5 tys. zł
Sign up to request clarification or add additional context in comments.

2 Comments

Wow!..What is that? Can search for div text in the 'xpath` itself?
@AbhishekRai you can select element with exact text or element that contains some partial text (div[contains(., "<PARTIAL_TEXT>")])
1

You are doing everything correct, just add break into the if statement to not overwrite finances to "Finance Data Not Available" after finding correct one:

fin_divs = driver.find_elements_by_css_selector('div.card.mb-4')
for div in fin_divs:
    if 'FINANSE' in div.text:
        finances = div.find_element_by_css_selector('div.card-body').text
        break
    else:
        finances = "Finance Data Not Available"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.