无法避免元素不可见异常,即使我正在使用 try and except 方法



我想从使用Selenium的网站下载数百万个excel文件。我当前的代码试图处理ElementNotVisibleException的问题,但我的"try and except"方法似乎不足。

我试图实现一个尝试和例外的解决方案,如果出现错误消息,我已经指示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:xxxchromedriver.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])
# THE LOOP
for ID in tqdm(bedrifter_gjenstår):
    driver.get("website" + ID)
    source = driver.page_source
    if not "Ingen data" in source: # make sure there is an excel file. If not, loop continues to next ID.
        # first click on button "download excel"
        try:
            driver.find_element_by_id("exportExcel").click()
        except ElementNotVisibleException:
            WebDriverWait(driver, 120).until(
                    EC.presence_of_element_located((By.ID, "exportExcel")))
            driver.find_element_by_id("exportExcel").click()
        except ElementClickInterceptedException:
            WebDriverWait(driver, 120).until(
                    EC.presence_of_element_located((By.ID, "exportExcel")))
            driver.find_element_by_id("exportExcel").click()
        # second click, choosing what format the excel file should be in
        try:
            driver.find_element_by_id("mainContentPlaceHolder_mainContentPlaceHolder_mainContentPlaceHolder_AccountingNumberTableUc_excelLinkButton").click()
        except ElementNotVisibleException:
            WebDriverWait(driver, 120).until(
                    EC.presence_of_element_located((By.ID, "mainContentPlaceHolder_mainContentPlaceHolder_mainContentPlaceHolder_AccountingNumberTableUc_excelLinkButton")))
            driver.find_element_by_id("mainContentPlaceHolder_mainContentPlaceHolder_mainContentPlaceHolder_AccountingNumberTableUc_excelLinkButton").click()
# code to switch between windows to remove download window and continue the code
        try: 
            window_export = driver.window_handles[1]
        except IndexError:
            time.sleep(3)
            print("sleep")
            window_export = driver.window_handles[1]
        try:
            window_main = driver.window_handles[0]
        except IndexError:
            time.sleep(3)
            print("sleep")
            window_main = driver.window_handles[0]
        driver.switch_to.window(window_export)
        driver.close()
        driver.switch_to.window(window_main)

我希望代码下载所有文件(如果有的话(,但出现 ElementNotVisibleException 或 ElementClickInterceptedException。

要确保元素不仅存在可见,而且可单击,请检查可操作性作为等待条件:

element = WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.ID, "exportExcel"))).click()

更多关于 Python 等待的信息可以在这里找到。

在对元素执行任何操作之前,请先检查它是否可见下面是示例代码:

wait = WebDriverWait(self.browser, 15)
wait.until(EC.visibility_of_element_located(("element_path")))
driver.find_element_by_xpath("element_path").click()
您可能会

遇到这些异常,因为它们发生在 except 子句中而不是try 内。请查看回溯以确定哪一行引发了异常。这应该告诉您问题出在哪里。此外,如果要等待元素可见,则应使用visibility_of_element_located而不是presence_of_element_located

相关内容

  • 没有找到相关文章

最新更新