i'm using a selenium code in spyder 3.8 with anaconda. The issue is that it works perfectly when i don't add any option to the driver, it goes to the url and click the thins it has to, but for some reason when i add options it just opens the browser, but doesn't go to the url and can't continue with the code. This is weird because i tried running this code on 2 other pc's with spyder 3.7 and an older version and it runs perfectly with the options.
I would really appreciate if someone could help me with this, thanks for your attention!
import pandas as pd
import zipfile
import os
import datetime
import shutil
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
from selenium.webdriver.common.by import By
import time
import win32com.client as win32
import re
import glob
from datetime import date, timedelta
from openpyxl import load_workbook #usar este en vez de pd para el loop
from dateutil.relativedelta import relativedelta
import win32com.client
from zipfile import ZipFile
driver_path = 'C:/Users/amoralesv/Desktop/dchrome/chromedriver'
link_gener = "https://www.coordinador.cl/operacion/graficos/operacion-real/generacion-real-del-sistema/"
link_SIC = "https://www.coordinador.cl/mercados/graficos/costos-marginales/costo-marginal-real/"
link_inf_nov = "https://www.coordinador.cl/operacion/documentos/novedades-cdc/"
link_inf_abast = "https://www.coordinador.cl/operacion/documentos/estudios-de-la-programacion-de-la-operacion/programacion-mensual/"
chrome_options = Options()
chrome_options.add_argument('--disable-extensions--')
chrome_options.add_argument("--headless")
chrome_options.add_experimental_option('prefs', {
"download.default_directory": "C:\\Users\\amoralesv\\Downloads",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True}) #Hace que el pdf se descargue y no se abra en pestaña
driver = webdriver.Chrome(executable_path = driver_path, options = chrome_options)
def descarga_generacion(mes, año, mes_pasado, año_pasado, driver):
driver.implicitly_wait(30)
driver.get(link_gener)
wait = W(driver, 20)
wait.until(E.element_to_be_clickable((By.ID, "tipo-xlsx"))).click()
time.sleep(1)
wait.until(E.element_to_be_clickable((By.ID, "tipo-xlsx"))).click()
time.sleep(9)
wait.until(E.element_to_be_clickable((By.ID, "datepicker777-9761_2"))).click()
dropdown_month = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "ui-datepicker-month"))))
time.sleep(1)
dropdown_month.select_by_visible_text(mes)
dropdown_year = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "ui-datepicker-year"))))
time.sleep(1)
dropdown_year.select_by_visible_text(año)
time.sleep(1)
wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui-priority-primary']"))).click()
time.sleep(1)
download = wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "a[class$='download-file-marginal']")))
driver.execute_script("arguments[0].click();", download)
time.sleep(5)
wait.until(E.element_to_be_clickable((By.ID, "datepicker777-9761_2"))).click()
dropdown_month = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "ui-datepicker-month"))))
time.sleep(1)
dropdown_month.select_by_visible_text(mes_pasado)
dropdown_year = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "ui-datepicker-year"))))
time.sleep(1)
dropdown_year.select_by_visible_text(año_pasado)
time.sleep(1)
wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui-priority-primary']"))).click()
time.sleep(1)
download = wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "a[class$='download-file-marginal']")))
driver.execute_script("arguments[0].click();", download)
def descarga_SIC_SING(nombre,nombre2, driver):
driver.implicitly_wait(30)
driver.get(link_SIC)
wait = W(driver, 20)
wait.until(E.element_to_be_clickable((By.ID, "datepicker22-9773"))).click()
time.sleep(2)
wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui-priority-primary']"))).click()
time.sleep(2)
wait.until(E.visibility_of_element_located((By.CLASS_NAME, "barras-costos"))).click()
dropdown_month = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "barras-costos"))))
dropdown_month.select_by_visible_text(nombre)
download = wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "a[class$='download-file-costos']")))
driver.execute_script("arguments[0].click();", download)
time.sleep(5)
wait.until(E.element_to_be_clickable((By.ID, "datepicker22-9773"))).click()
time.sleep(2)
wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui-priority-primary']"))).click()
time.sleep(2)
wait.until(E.visibility_of_element_located((By.CLASS_NAME, "barras-costos"))).click()
dropdown_month = Select(wait.until(E.visibility_of_element_located((By.CLASS_NAME, "barras-costos"))))
dropdown_month.select_by_visible_text(nombre2)
download = wait.until(E.element_to_be_clickable((By.CSS_SELECTOR, "a[class$='download-file-costos']")))
driver.execute_script("arguments[0].click();", download) ```
The problematic entry in your options is:
"plugins.always_open_pdf_externally": True
I'd recommend use this one instead:
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}]
It does the same thing; allows pdf files to be downloaded instead of showing them.