我想使用 selenium 从网站下载数百万个 excel 文件。我当前的代码尝试处理 ElementNotVisibleException 的问题,但我的“尝试和例外”方法似乎不足。
我试图实施一个尝试和例外解决方案,如果出现错误消息,我已指示 Selenium 等到“按钮”出现。
import os
import time
import csv
from tqdm import tqdm
import pandas as pd
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import ElementClickInterceptedException
working_directory = r"xx"
os.chdir(working_directory)
options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": r"xx",
"download.prompt_for_download": False,
"download.directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(r"C:xxx\chromedriver.exe", options = options)
driver.get("website")
# Login
driver.find_element_by_class_name("smallLoginBox").click()
driver.implicitly_wait(1)
time.sleep(2)
driver.find_element_by_id('loginFormUC_loginEmailTextBox').send_keys('EMAIL')
driver.find_element_by_id('loginFormUC_loginPasswordTextBox').send_keys('PASWORD')
driver.find_element_by_xpath("//input[@value='Logg inn']").click()
# Get a custom list of firms
bedrifter = []
with open("./listwithIDs.csv") as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
bedrifter.append(row[0])
我希望代码能够下载所有文件(如果有),但会出现 ElementNotVisibleException 或 ElementClickInterceptedException。
杨__羊羊
慕勒3428872
紫衣仙女
相关分类