Search code examples
pythonseleniumweb-scrapingamcharts

AmCharts Scraping Using Python Selenium


I am trying to Scrape AmCharts Graph Values in JSON Object from this URL

Using the following command

driver.execute_script("AmCharts.charts[0].dataProvider")

In the script, it returns None while the browser console returns JSON object with the AmCharts Data

enter image description here

It appears like this in the web interface enter image description here

How I can retrieve this dataProvider Array correctly. Thanks in advance.


Solution

  • You need to add return to your execute_script call in order to access the value in your script, e.g. driver.execute_script("return AmCharts.charts[0].dataProvider")

    Full code below:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    
    driver_path = r"path/to/chromedriver"
    
    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    
    driver.get("https://eg.pricena.com/en/product/oppo-reno-5g-price-in-egypt")
    
    # scroll into the div so that the chart will render
    driver.execute_script("document.getElementById('product_pricechart').scrollIntoView()")
    
    # wait until the chart div has been rendered before accessing the data provider
    wait.until(lambda x: x.find_element_by_class_name("amcharts-chart-div").is_displayed())
    
    # display chart data
    print(driver.execute_script("return AmCharts.charts[0].dataProvider"))
    
    driver.close()