2

I'm trying to click a button to automatically export a csv file from this site. I've tried finding the element by button name and by Xpath, but the "NoSuchElement" Exception is raised. I've tried variations of WebdriverWait and time.sleep to ensure the button loads, and used the xPath finder extension to get the right xPath.

Code is as follows:

#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()

The button I'm trying to access is shown in image below: enter image description here Before posting I've referenced the following Questions:

  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'

*new to Selenium and not well versed in HTML. Any insight is much appreciated.

Matt
  • 55
  • 6

2 Answers2

1

I was able to click that button using SeleniumBase.

pip install seleniumbase, save the following script to a file, and then run with python or pytest:

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)

The self.highlight() command highlights the button to show it was found. And then it gets clicked.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • I get a name error '__file__' is not defined. But it looks like this is the right syntax (https://seleniumbase.io/). Any thoughts on what might be causing that? – Matt Jun 09 '23 at 01:06
  • 1
    If you are calling ``__file__`` when inside a file, it points to it. But alternatively, remove the ``BaseCase.main(__name__, __file__)`` line and call the script directly with ``pytest`` instead of ``python``. – Michael Mintz Jun 09 '23 at 01:08
  • _I get a name error `'__file__'` is not defined_ Are you running this code in a script, or inside an IDE/interpreter? – John Gordon Jun 09 '23 at 01:13
  • Was running within jupyter notebook ipynb file – Matt Jun 09 '23 at 01:14
  • It has to be part of a script. If you just type ``python`` and call it from there, ``__file__`` is not defined because it isn't directly in a file. – Michael Mintz Jun 09 '23 at 01:14
  • Assuming ``pytest`` worked for you? – Michael Mintz Jun 09 '23 at 01:15
1

You can easily do it using Selenium only.

Here's how:

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)

output:

The file is downloaded!

steps to follow:

  1. As the site takes some time to load the desired element (here, the Export button). And clicking on this button downloads the data of the table. Therefore we wait to make sure that the table data is already loaded.

  2. Now that the data is already loaded, simply click on the Export button to download the data (here Facilities.csv).

  3. It takes some time for the file to get downloaded at the given path, so we need to wait until the file download is completed. To do this, we keep checking if the file is present at the given path, and once the file is there, we break the loop.

I hope it solves your problem.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24