debugging Selenium无法通过Xpath查找元素?

vhmi4jdf  于 2023-06-23  发布在  其他
关注(0)|答案(2)|浏览(119)

我正在尝试单击一个按钮以从this site自动导出csv文件。我尝试通过按钮名称和XPath查找元素,但引发了“NoSuchElement”异常。我尝试了WebdriverWait和time.sleep的变体来确保按钮加载,并使用xPathfinder扩展来获得正确的xPath。
代码如下:

#import Packages
import selenium
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

#set URL, Get it, and wait
driver = webdriver.Chrome()
driver.get('https://www.egle.state.mi.us/RIDE/inventory-of-facilities/facilities')
WebDriverWait(driver,11)

#Set and Click button 
button = driver.find_element(by=By.XPATH, value='//*[@id="maincontent"]/div/inventory-of-facilities/som-page-section/div/div[2]/div[2]/facilities-table/som-page-section/div/div[2]/div[2]/som-table/div/div/div[1]/div[2]/div[2]/button')
button.click()

我尝试访问的按钮如下图所示:

在发布之前,我已经参考了以下问题:

  1. Clicking a button with Selenium button
  2. How to press/click the button using Selenium if the button does not have the Id?
  3. AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
  • 刚接触Selenium,不太熟悉HTML。任何见解都非常赞赏。
vyu0f0g1

vyu0f0g11#

我可以使用SeleniumBase单击该按钮。
pip install seleniumbase,将以下脚本保存到文件中,然后使用pythonpytest运行:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class ClickButtonTest(BaseCase):
    def test_click_a_button(self):
        self.open("https://www.egle.state.mi.us/RIDE/inventory-of-facilities/facilities")
        self.highlight('button[aria-label="Export Facilities Table results to CSV"]')
        self.click('button[aria-label="Export Facilities Table results to CSV"]')
        self.sleep(5)

self.highlight()命令高亮显示按钮以显示找到了它。然后点击。

8iwquhpp

8iwquhpp2#

你可以简单地使用Selenium
具体操作如下:

import os
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

def download_file(path):

    options = ChromeOptions()
    options.add_argument('--start-maximized')
    prefs = {'download.default_directory': path}
    options.add_experimental_option('prefs', prefs)

    driver = Chrome(options=options)
    driver.get('https://www.egle.state.mi.us/RIDE/inventory-of-facilities/facilities')
    wait = WebDriverWait(driver, 100)

    wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'mat-table')))
    driver.execute_script('''document.querySelector("button[aria-label='Export Facilities Table results to CSV']").click();''')

    while True:
        is_exist = os.path.exists(f"{path}\\Facilities.csv")
        if is_exist:
            print(f"The file is downloaded!")
            break

PATH = 'D:\\test'
download_file(PATH)

输出:

The file is downloaded!

要遵循的步骤:
1.因为站点需要一些时间来加载所需的元素(这里是Export按钮)。单击此按钮下载表中的数据。因此,我们等待以确保表数据已经加载。
1.现在数据已经加载完毕,只需单击Export按钮即可下载数据(此处为Facilities.csv)。
1.在给定路径下载文件需要一些时间,所以我们需要等待文件下载完成。要做到这一点,我们不断检查文件是否存在于给定的路径,一旦文件存在,我们就中断循环。
我希望它能解决你的问题。

相关问题