To properly click the button and download the file, you need to perform the following steps:
Locate the button element: You can use the find_element_by_css_selector
method to locate the button element based on the class name and text. The updated code would look like this:
gotit = driver.find_element_by_css_selector('a.calibre-push-button span:contains("Download")')
Click the button: Once you have located the button element, you can use the click
method to click the button and initiate the download process.
gotit.click()
Wait for the download to complete: The download may take some time to complete, so you need to wait for it to finish before you can save the file. You can use the WebDriverWait
class and the expected_conditions
module to wait for the file to download.
Here's an example:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ... other code ...
# Click the button
gotit.click()
# Wait for the file to download
wait = WebDriverWait(driver, 30)
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, '.txt')))
- Save the file: After the download has completed, you can use the
os
module to save the file to your desired location.
Here's an example:
import os
# ... other code ...
# Save the file
download_path = '/path/to/download/folder'
for f in os.listdir(download_path):
if f.endswith('.txt'):
file_path = os.path.join(download_path, f)
with open(file_path, 'wb') as f:
f.write(requests.get(gotit.get_attribute('href')).content)
Here is another solution for downloading the file after clicking the button using Selenium and Requests:
from selenium import webdriver
import requests
driver = webdriver.Chrome(executable_path='chromedriver')
driver.get('http://website.address.com')
gotit = driver.find_element_by_xpath("//a.calibre-push-button [contains(text(),'Download')]")
gotit.click()
file_url = gotit.get_attribute('href')
file_name = gotit.get_attribute('download')
file_path = f"{file_name}"
with open(file_path, 'wb') as f:
f.write(requests.get(file_url).content)
driver.quit()
This solution opens a Chrome browser, navigates to the specified URL, locates the button element with the given class name and text, clicks it to initiate the file download, retrieves the file URL and name from the button's href
and download
attributes, and finally writes the file content to the specified file path using the Requests library.