[Python] Unable to locate element when attempting to extract a text in Python using Selenium

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get all the text contents under bloomberg news library. Below code uses one example url.

    from selenium import webdriver
    from fake_useragent import UserAgent
    from selenium.webdriver.common.by import By
    from selenium.webdriver.firefox.options import Options

    # Chrome Path
    ua = UserAgent()
    userAgent = ua.random
    options = Options()
    #options.add_argument('--headless')
    options.add_argument(f'user-agent={userAgent}')
    browser = webdriver.Firefox(options=options)
    input_url = "https://www.bloomberg.com/news/articles/2015-11-06/the-true-story-of-soul-city-a-utopian-town-built-for-african-americans-with-republican-support-in-the-early-1970s?srnd=citylab-housing"
    print(input_url)
    browser.get(input_url)

    element = browser.find_element(by=By.CLASS_NAME, value='body-content fence-body')

    # Get Text
    print(element.text)

    # Close the window
    browser.close()


    However, I get Unable to locate element: .body-content fence-body when there is that tag within the HTML file. Why is that?

    Continue reading...

Compartilhe esta Página