0

I have a website for which I want to download excel files. (https://www.rivm.nl/media/smap/eenzaamheid.html)

First I want to click on the region and then perform the download. This I have working.

wijk_keuze = WebDriverWait(driver2nd,20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='highcharts-container ']//*[name()='svg']//*[name()='g']//*[name()='path']")))
wijk_keuze.click()
download = WebDriverWait(driver2nd, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='highcharts-container ']//*[name()='svg']//*[name()='g' and @aria-label='View export menu']//*[name()='rect']")))  
download.click()
WebDriverWait(download, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='highcharts-menu']//*[contains(text(),'XLS downloaden')]"))).click()
time.sleep(2)

above code selects the first region in the parent element and then downloads the excel. What I want to do is loop through each element in the parent element. How would I go about doing so?

The parent element looks as follows:

<g transform="transform(0,0), scale(1,1)" animator="1">

my entire code:

 from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

#defining URL
url='https://www.rivm.nl/media/smap/eenzaamheid.html'

#defining driver
driver = webdriver.PhantomJS(r'./phantomjs-2.1.1-windows/bin/phantomjs')
options = webdriver.ChromeOptions()
options.add_argument("start-maximized");
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options, executable_path = r'./chromedriver_win32/chromedriver')
driver.get(url)

# Gemeentes 

Detail_keuze = Select(driver.find_element_by_id("detail"))
options = Detail_keuze.options
Indicator_keuze = Select(driver.find_element_by_id("indicator"))
indicator_options = Indicator_keuze.options


for index in range(0, len(indicator_options) ):
    #defining URL
    url='https://www.rivm.nl/media/smap/eenzaamheid.html'

    #defining driver
    driver2nd = webdriver.PhantomJS(r'./phantomjs-2.1.1-windows/bin/phantomjs')
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized");
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_experimental_option("prefs", {
        "download.default_directory": r"MY_PATH",
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing.enabled": True
        })
    driver2nd = webdriver.Chrome(options=options, executable_path = r'./chromedriver_win32/chromedriver')
    driver2nd.get(url)

    # Gemeentes 
    Detail_keuze = Select(driver2nd.find_element_by_id("detail"))
    options = Detail_keuze.options
    Indicator_keuze = Select(driver2nd.find_element_by_id("indicator"))
    indicator_options = Indicator_keuze.options
    time.sleep(1)

    Indicator_keuze.select_by_index(index)
    wijk_keuze = WebDriverWait(driver2nd,20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='highcharts-container ']//*[name()='svg']//*[name()='g']//*[name()='path']")))
    wijk_keuze.click()

    download = WebDriverWait(driver2nd, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='highcharts-container ']//*[name()='svg']//*[name()='g' and @aria-label='View export menu']//*[name()='rect']")))  
    download.click()
    WebDriverWait(download, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='highcharts-menu']//*[contains(text(),'XLS downloaden')]"))).click()
    time.sleep(2)
######## HERE I WANT TO LOOP THROUGH EACH AND EVERY REGION
    driver2nd.close()  
    

As you can see I also want to loop through eachh and every indicator. This works. Now I want to add a loop through each and every region. I have it working so that I can click on the first region.

2
  • I am not able to run your code. I guess you can select all the elements instead of selecting one and then use comprehension lists in python to apply a particular functionality to every element on this list. Commented Jul 23, 2020 at 15:13
  • I edited my question and added my entire code, hope this helps in finding a solution Commented Jul 23, 2020 at 15:19

1 Answer 1

2

You don't have to click on the options. You can get the details by changing the url 'https://www.rivm.nl/media/smap/{indicator}?detail={detail}' Just add the logic for downloading it.

Try:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from itertools import product

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install())#, chrome_options=chrome_options)
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get('https://www.rivm.nl/media/smap/eenzaamheid.html')
time.sleep(5)
soup  = BeautifulSoup(driver.page_source, 'html.parser')
ind = soup.find('select', attrs = {'name': 'indicator'} )
indicators = [i['value'] for i in ind.findAll('option')]

det = soup.find('select', attrs = {'name': 'detail'})
details = [i['value'] for i in det.findAll('option')]
for detail, indicator in list(product(details, indicators)):
    print(indicator, detail)
    new_url = f'https://www.rivm.nl/media/smap/{indicator}?detail={detail}'
    driver.get(new_url)
   # Write code for downloading it 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.