简体   繁体   English

如何使Python / Selenium等待文件处理,然后单击下载

[英]How can I make Python/Selenium wait for file to process then click download

I'm trying to use Python/Selenium to automate the process of downloading a file that is updated daily. 我正在尝试使用Python / Selenium来自动化下载每天更新的文件的过程。 My code so far opens the website, selects the criteria I want (Downloading file in CSV format), and then clicks the "Start Processing" button. 到目前为止,我的代码打开了网站,选择了我想要的条件(下载CSV格式的文件),然后单击“开始处理”按钮。 The way the website is set up, after you click "Start Processing", the site redirects you to a different page with a loading bar (indicating the completion of the processing) and a "cancel request" button, and after about ~10 seconds the file is ready and the "cancel request" button turns into a "Download my File" button. 单击“开始处理”后,网站的设置方式将通过带有加载栏(指示处理完成)和“取消请求”按钮的网站将您重定向到其他页面,大约10秒钟后文件已准备就绪,“取消请求”按钮变为“下载我的文件”按钮。 The file will progressively become larger as the year goes on so processing will go from ~10 seconds to a few (2-4) minutes. 随着年份的延长,文件将逐渐变大,因此处理时间将从约10秒缩短至几(2-4)分钟。 The processing will auto time out if it takes longer than 5 minutes. 如果时间超过5分钟,处理将自动超时。 My "#Supposed to wait for the file to process" attempt was intended to wait until the ID "btn_download" (ID of the "Download my File" click) showed up before clicking the "Download my File" link. 我的“#应该等待文件处理”尝试是要等到ID“ btn_download”(单击“ Download my File”的ID)出现后再单击“ Download my File”链接。 I think the code I have written to download the file will work since that same code worked to begin processing and they're both "Type: submit" links, but I am not sure if the reason I am not getting a download is because the "wait" code is not working properly, or if it is the "download code" (or both). 我认为我编写的用于下载文件的代码将起作用,因为该代码可以开始处理,并且它们都是“类型:提交”链接,但是我不确定是否无法下载的原因是因为“等待”代码无法正常工作,或者它是否是“下载代码”(或两者)。 I tried using "delay = x", driver.implicitly_wait(x), and "time.sleep(x)" and they did not work for me (I may have used them incorrectly I'm not sure). 我尝试使用“ delay = x”,driver.implicitly_wait(x)和“ time.sleep(x)”,但它们对我不起作用(我不确定使用它们是否正确)。 Ultimately the goal is to open the page, select the CSV criteria, begin processing, wait until processing is complete, download file, and save it to a folder in my hard drive (as opposed to leaving it in my downloads) 最终目标是打开页面,选择CSV标准,开始处理,等待处理完成,下载文件,然后将其保存到硬盘中的文件夹中(而不是将其保留在下载中)

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

#Opens the website
browser = webdriver.Firefox() 
browser.get("https://mywebsite.com")

#Clicks CSV Option
browser.find_element_by_id("rdo_file").click()

#Starts Processing
browser.find_element_by_id("btnStart").click()

#Supposed to wait for the file to process 
try:
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "btn_download"))
)

#Supposed to click the "Download my File" link
browser.find_element_by_id("btn_download").click()

Bonus points if you can help me download the file to a specific file in my harddrive! 如果您可以帮助我将文件下载到硬盘中的特定文件,则可加分! Thanks for the help! 谢谢您的帮助!

Edited to add error message: 编辑以添加错误消息:

File "<stdline>" line 6
^
Syntax Error: invalid syntax

Also just realized that after that error message, Python allows me to hit enter again to process the "Download my File" code, which does work. 还刚刚意识到,在显示该错误消息之后,Python允许我再次按Enter键以处理“下载我的文件”代码,该代码确实有效。 So my problem lies with the wait code (need it to wait and continue the process automatically so I do not need to go back in and hit enter). 因此,我的问题出在等待代码上(需要它等待并自动继续执行该过程,因此我无需返回并按Enter键)。

from selenium.webdriver.support import expected_conditions as EC

try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "btn_download"))
    )
except:
    wait = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'btn_download')))
browser.find_element_by_id("btn_download").click()

As per your observation as the problem lies with the wait code and as per the HTML you have provided to click the button to start downloading the file you can use either of the following lines of code : 根据您的观察, 问题出在等待代码上 ,按照HTML提供的内容 ,您可以单击按钮开始下载文件,可以使用以下任一代码行:

  • CSS_SELECTOR & click() : CSS_SELECTORclick()

     WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btn_download[name='btn_download'][value='Download My File']")).click() 
  • CSS_SELECTOR & submit() : CSS_SELECTORCSS_SELECTOR submit()

     WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btn_download[name='btn_download'][value='Download My File']")).submit() 
  • XPATH & click() : XPATHclick()

     WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btn_download' and @name='btn_download' and @value='Download My File']")).click() 
  • XPATH & submit() : XPATHXPATH submit()

     WebDriverWait(driver, 300).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btn_download' and @name='btn_download' and @value='Download My File']")).submit() 

Note : As you mentioned the auto timeout is 5 minutes I have kept the maximum wait to equivalent to 300 seconds 注意:正如您提到的,自动超时为5分钟,我将最大等待时间保持为等于300秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM