Selenium - 找到了处于两种状态的按钮元素(“下载”): 1. aria-hidden="true" 2. 我想与之交互但不能交互的元素

问题描述 投票:0回答:1

背景: 我正在尝试测试我的整体程序来下载每周都会更改的 Excel 文件,然后分析差异。我将 Excel 文件托管在 Google Drive 中(我希望这不会引入独一无二的问题)。

问题: 我目前陷入了 Python 代码的下载部分。我认为 Google Drive 有两个允许下载的元素。 (1) 我正确找到的下载图标(我怎么知道:我在 Chrome 控制台中使用了 JavaScript,它完美地执行了下载)。 (2) 3 点菜单有一个名为“下载”的子菜单项,我无法定位(找到)该子菜单项。 Inspect Element 在子菜单中找不到 Download 操作;这是更难尝试解决的路线,因此我专注于路线 (1)。

我所实施的: 记录 任意超时 预期条件 经过 WebDriver等待 行动链 我也检查过框架。它似乎不在其中,但我可能是错的。

这是我的代码:

import os
import time # Insert a pause before performing a click
from selenium import webdriver
import logging  # Import logging for logging setup
import configparser  # Import configparser for configuration file handling
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 NoSuchElementException, TimeoutException  # Include TimeoutException
import undetected_chromedriver as uc
from selenium.webdriver import ActionChains

# Set up logging
logging.basicConfig(filename='logging.log', level=logging.INFO)

# Load configuration file for using credentials
config = configparser.ConfigParser()
config.read('credentials.ini')

username = config['credentials']['username']
password = config['credentials']['password']

# Download directory path
download_dir = '/path/to/directory'  # My real code has the correct path.

# Set up Chrome driver
def setup_driver():
    options = uc.ChromeOptions()
    prefs = {'download.default_directory': download_dir}
    options.add_experimental_option("prefs", prefs)
    options.add_argument("--start-maximized")
    options.add_argument("--disable-blink-features=AutomationControlled")  # Additional undetectable options
    

    try:
        driver = uc.Chrome(options=options)
        return driver
    except Exception as e:
        logging.error(f"Failed to initialize undetected_chromedriver: {e}")
        raise

def main():
    driver = setup_driver()
    try:
        # Navigate to the website
        driver.get('https://drive.google.com/drive/home')
        logging.info("Navigated to login page successfully.")
        
        try:
            # Enter Username

            .....continued.

        # Wait for the page to load after login
        time.sleep(5)  # Add a small delay before trying to find the download button.

        # Log the current page source for debugging
        # logging.info(driver.page_source)  # Log the page source for debugging

        # Try to find the download button
        try:
            download_button = WebDriverWait(driver, 20).until(
                EC.visibility_of_element_located((By.XPATH, "//button[@aria-label='Download']"))
            )

            # Debugging: Log the button's attributes and state
            logging.info(f"Download button found: {download_button.get_attribute('outerHTML')}")
            logging.info(f"Button displayed: {download_button.is_displayed()}")
            logging.info(f"Button enabled: {download_button.is_enabled()}")

            # Check if the button is interactable before clicking
            if download_button.is_displayed() and download_button.is_enabled():
                # Use ActionChains to click the button
                actions = ActionChains(driver)
                actions.move_to_element(download_button).click().perform()  # Move to the button and click
                logging.info("Clicked the download button using ActionChains.")
                # Adding a wait after clicking the button
                time.sleep(10)  # Wait for a few seconds to see if the download initiates

            else:
                logging.error("Download button is not interactable.")
        except NoSuchElementException:
            logging.error("Download button not found.")
        except TimeoutException:
            logging.error("Timed out waiting for the download button to become clickable.")
        except Exception as e:
            logging.error(f"Error clicking download button: {e}", exc_info=True)


    finally:
        # Close the browser
        driver.quit()

if __name__ == "__main__":
    main()

我期待下载执行。就像控制台中的 JavaScript 代码一样,但使用 Python 等效的 Try except 语句块执行。

这是有效的 JavaScript:

document.querySelector('button[aria-label="Download"]').click();

我认为正在发生的事情是,当转到路线 1 时,下载图标会出现两种状态。似乎有两个元素与“下载”功能相关。这是我发现的内容的细分。

  1. 第一个元素: 该元素具有 aria-hidden="true" 和 aria-disabled="true",其样式设置为 display: none;。我相信这意味着它目前不可见且不可交互。

div class="h-sb-Ic h-R-d h-R-d-db a-c-d a-s-Ba-d-Mr-Be-nAm6yf" role="button" aria-hidden="true" aria-disabled="true" data-tooltip="Download" aria-label="Download" ... style="user-select: none; display: none;"

  1. 第二个元素: 该元素是一个带有 aria-label="Download" 的按钮,似乎是我应该与之交互的按钮。然而,它似乎也有一些通过 JavaScript 设置的复杂交互。

button class="pYTkkf-c-d ... aria-label="Download" ...

该问题可能源于以下事实:第一个元素阻止与第二个元素的交互,或者第二个元素可能不处于允许单击的状态(例如,它不可见或不启用)。尝试强制明确的等待时间并没有奏效。我等了10秒到60秒。

selenium-webdriver
1个回答
0
投票

您是否在找到下载按钮之前选择了文件? 由于只有选择文件时才会出现(显示)路线(1)下载按钮。

© www.soinside.com 2019 - 2024. All rights reserved.