1

I want to wait for a page to finish converting a json file, then automatically download it. The following python code works.

import time
from selenium import webdriver

chrome = webdriver.Chrome()
chrome.get('https://json-csv.com/')    
load_data = chrome.find_element_by_id('fileupload') 
load_data.send_keys('C:\\path_to_file')
load_data.submit()

# Wait arbitrary duration before downloading result
time.sleep(10) 
get_results = chrome.find_element_by_id('download-link')
get_results.click()
chrome.quit()

However, every time I run the script, I need to wait 10 seconds, which is more than enough for the page to finish converting the file. This is not time efficient. The page may finish loading the new file in 5 seconds.

How can I click the download button the moment the file is done converting?

What I've tried

I've read a solution to a similar problem, but it threw an error: ElementNotVisibleException: Message: element not visible.

Also tried following the documentation example:

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

...

wait = WebDriverWait(chrome, 10)
get_result = wait.until(EC.element_to_be_clickable((By.ID, 'download-link')))
get_result.click()

This downloads some nonsense .tmp file instead.

angsty_robot
  • 305
  • 2
  • 6
  • 13

2 Answers2

1

You need to make a small change in the approach as follows:

Rather waiting for the WebElement as By.ID, 'download-link' with clause element_to_be_clickable, I would suggest you to try to wait for the WebElement as By.ID, 'convert-another' with clause element_to_be_clickable and then click on the DOWNLOAD link as follows:

wait = WebDriverWait(chrome, 10)
wait.until(EC.element_to_be_clickable((By.ID, 'convert-another')))
chrome.find_element_by_css_selector("a#download-link.btn-lg.btn-success").click()
chrome.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your code is ok. The exception is because you call load_data.submit() after the load_data.send_keys('C:\\path_to_file').

Remove this line:

chrome.get('https://json-csv.com/')
load_data = chrome.find_element_by_id('fileupload')
load_data.send_keys('C:\\path_to_file')


wait = WebDriverWait(chrome, 10)
get_result = wait.until(EC.element_to_be_clickable((By.ID, 'download-link')))
get_result.click()
Davide Patti
  • 3,391
  • 2
  • 18
  • 20
  • Retried with your edit, unfortunately it still returns the .tmp file. Additionally, I also noticed that the script doesn't appear to be "waiting" -- it immediately downloads a tmp file and exits. – angsty_robot Nov 02 '17 at 09:01