Abhishek Rai
Abhishek Rai

Reputation: 2227

Having trouble getting some text. Python. Selenium

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"

Upvotes: 0

Views: 61

Answers (2)

JaSON
JaSON

Reputation: 4869

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ł

Upvotes: 1

Rustam Garayev
Rustam Garayev

Reputation: 2692

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"

Upvotes: 1

Related Questions