首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在下载之前保存文件并将其移动到所需位置(python、selenium)?使用chrome webdriver

如何在下载之前保存文件并将其移动到所需位置(python、selenium)?使用chrome webdriver
EN

Stack Overflow用户
提问于 2020-02-23 00:37:35
回答 1查看 40关注 0票数 0

我是selenium chrome webdriver的新手。我可以访问一个网页,在那里我输入我的凭证,然后我有一个按钮,我点击它下载文件作为csv (它直接到我的下载文件夹)。

理想情况下,我希望将下载的csv文件发送到另一个文件夹并重命名它。

我的代码如下:

代码语言:javascript
复制
    from selenium import webdriver
from datetime import date
d = datetime.datetime.now() 
dayprueba="%d/%d/%d"%(d.month, d.day, d.year)

driver = webdriver.Chrome()

#acceder a pagina web
driver.get ("MY URL")


emailbox = driver.find_element_by_xpath('//*[@id="mat-input-0"]') #encuentra pagina web
emailbox.send_keys('MY EMAIL') #escribe el usuario

passwordbox = driver.find_element_by_xpath('//*[@id="mat-input-1"]')
passwordbox.send_keys('MY PASS')


botondeingreso = driver.find_element_by_xpath('/html/body/app-root/app-manual-login/div/div/div/form/button')
botondeingreso.click()

sleep(2)

botonreportes = driver.find_element_by_xpath("/html/body/app-root/app-dashboard/div/div[2]/div/div[5]/wui-dashboard-tile/div/div/div[3]/wui-button/button")
botonreportes.click()

#bajado de tablas de argentina
#bajado de tabla de orders

botonfechaargentina = driver.find_element_by_xpath('//*[@id="mat-input-2"]')
botonfechaargentina.send_keys('1/1/2018')

sleep(1)


flechaprov = driver.find_element_by_xpath('//*[@id="mat-select-3"]/div/div[2]')
flechaprov.click()
sleep(1)

botonprovinciasargentinas= driver.find_element_by_xpath('//*[@id="mat-option-12"]/span')
botonprovinciasargentinas.click()

sleep(1)

botondefechadehoy= driver.find_element_by_xpath('//*[@id="mat-input-3"]')
botondefechadehoy.send_keys(dayprueba)

clickearafueradelapantalla= driver.find_element_by_xpath('/html/body/app-root/app-reports/div')
clickearafueradelapantalla.click()

botondedescargaAR = driver.find_element_by_xpath('/html/body/app-root/app-reports/div/form/div[4]/button')
botondedescargaAR.click()

在最后一步中,单击按钮可以正常工作,但下载内容直接转到C:\Users\barra\Downloads (这是我的下载文件夹,希望将其移动到另一个文件夹)。

感谢您的帮助!

谢谢

The image show the name of downloaded file, I want to change it to orders_ar, everything from the first "_" I want to remove and change for "ar"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-23 10:38:17

您可以使用chrome首选项更改chromedriver下载路径,如下所示。

代码语言:javascript
复制
from selenium.webdriver.chrome.options import Options

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/your/desired/path"}
chromeOptions.add_experimental_option("prefs",prefs)
``
Make sure you add these line before `driver = webdriver.Chrome(chrome_options=chrome_options)`
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60354319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档