我的 Python Selenium 代码不适用于选项
My Python Selenium code doesn't work with options
我在 spyder 3.8 中使用 selenium 代码和 anaconda。问题是,当我不向驱动程序添加任何选项时,它会完美运行,它会转到 url 并单击它必须单击的内容,但由于某种原因,当我添加选项时,它只会打开浏览器,但不会转到 url 并且无法继续代码。这很奇怪,因为我在其他 2 台装有 spyder 3.7 和旧版本的电脑上 运行 尝试了这段代码,并且它与选项完美运行。
如果有人能帮助我,我将不胜感激,感谢您的关注!
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) ```
您的选项中有问题的条目是:
"plugins.always_open_pdf_externally": True
我建议改用这个:
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}]
它做同样的事情;允许下载 pdf 文件而不是显示它们。
我在 spyder 3.8 中使用 selenium 代码和 anaconda。问题是,当我不向驱动程序添加任何选项时,它会完美运行,它会转到 url 并单击它必须单击的内容,但由于某种原因,当我添加选项时,它只会打开浏览器,但不会转到 url 并且无法继续代码。这很奇怪,因为我在其他 2 台装有 spyder 3.7 和旧版本的电脑上 运行 尝试了这段代码,并且它与选项完美运行。
如果有人能帮助我,我将不胜感激,感谢您的关注!
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) ```
您的选项中有问题的条目是:
"plugins.always_open_pdf_externally": True
我建议改用这个:
"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}]
它做同样的事情;允许下载 pdf 文件而不是显示它们。