How to rename downloaded file using selenium c# webdriver
In my web application which has some automation process to download the files from the website. To achieve that I used selenium c# chrome driver.
Sample code snippets
public void Download(string name,string pass)
{
try
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=http://192.168.5.62:8095");
options.AddUserProfilePreference("safebrowsing.enabled", true);
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddUserProfilePreference("download.default_directory",@"C:Temp");
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)){
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
}
}
catch (Exception ex)
{
Logger.LogWriter("LAS", ex, "CusDataLogic", "Download");
}
}
above code (not complete code) works fine and save file properly. But I need to rename that file downloading or after download. Have any possible way to rename that file?
Edited: Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
c# asp.net-mvc selenium selenium-chromedriver
add a comment |
In my web application which has some automation process to download the files from the website. To achieve that I used selenium c# chrome driver.
Sample code snippets
public void Download(string name,string pass)
{
try
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=http://192.168.5.62:8095");
options.AddUserProfilePreference("safebrowsing.enabled", true);
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddUserProfilePreference("download.default_directory",@"C:Temp");
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)){
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
}
}
catch (Exception ex)
{
Logger.LogWriter("LAS", ex, "CusDataLogic", "Download");
}
}
above code (not complete code) works fine and save file properly. But I need to rename that file downloading or after download. Have any possible way to rename that file?
Edited: Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
c# asp.net-mvc selenium selenium-chromedriver
1
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
2
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58
add a comment |
In my web application which has some automation process to download the files from the website. To achieve that I used selenium c# chrome driver.
Sample code snippets
public void Download(string name,string pass)
{
try
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=http://192.168.5.62:8095");
options.AddUserProfilePreference("safebrowsing.enabled", true);
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddUserProfilePreference("download.default_directory",@"C:Temp");
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)){
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
}
}
catch (Exception ex)
{
Logger.LogWriter("LAS", ex, "CusDataLogic", "Download");
}
}
above code (not complete code) works fine and save file properly. But I need to rename that file downloading or after download. Have any possible way to rename that file?
Edited: Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
c# asp.net-mvc selenium selenium-chromedriver
In my web application which has some automation process to download the files from the website. To achieve that I used selenium c# chrome driver.
Sample code snippets
public void Download(string name,string pass)
{
try
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=http://192.168.5.62:8095");
options.AddUserProfilePreference("safebrowsing.enabled", true);
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddUserProfilePreference("download.default_directory",@"C:Temp");
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)){
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
}
}
catch (Exception ex)
{
Logger.LogWriter("LAS", ex, "CusDataLogic", "Download");
}
}
above code (not complete code) works fine and save file properly. But I need to rename that file downloading or after download. Have any possible way to rename that file?
Edited: Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
c# asp.net-mvc selenium selenium-chromedriver
c# asp.net-mvc selenium selenium-chromedriver
edited Nov 26 '18 at 7:40
Sachith
asked Nov 26 '18 at 5:25
SachithSachith
1,84321429
1,84321429
1
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
2
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58
add a comment |
1
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
2
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58
1
1
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
2
2
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58
add a comment |
1 Answer
1
active
oldest
votes
watching directory is not always good, because sometime saved filename is different than filename in URL.
go to chrome download page and loop until all download complete, you can see below how to select special element #shadow-root
with CSS selector
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)))
{
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
// wait 5 second until download started
Thread.Sleep(5000);
// Go to chrome download page
driver.Navigate().GoToUrl("chrome://downloads/");
string oldName = "";
bool downloadcomplete = false;
string cssNames = "downloads-manager /deep/ downloads-item /deep/ [id='name']";
string cssDlProgress = "downloads-manager /deep/ downloads-item /deep/ [class*='show-progress']";
while (!downloadcomplete)
{
var progressElements = driver.FindElements(By.CssSelector(cssDlProgress));
// check until no download progress bar
if (progressElements.Count() == 0)
{
oldName = driver.FindElement(By.CssSelector(cssNames)).Text;
downloadcomplete = true;
}
else
{
// download still in progress, wait.
Thread.Sleep(1000);
}
}
// download complete
// remove downloaded file
driver.FindElement(By.CssSelector("downloads-manager /deep/ downloads-item /deep/ [id='remove']")).Click();
// rename
File.Move(@"C:Temp" + oldName, @"C:Tempnewname.ext");
}
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called insideforloop
. based on name and password file will be downloaded. so how to rename each file after download
– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this pathC:Temp
. But not showing downloaded file inchrome://downloads/
menu
– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and clickid="remove"
to clear download list
– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
|
show 10 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475157%2fhow-to-rename-downloaded-file-using-selenium-c-sharp-webdriver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
watching directory is not always good, because sometime saved filename is different than filename in URL.
go to chrome download page and loop until all download complete, you can see below how to select special element #shadow-root
with CSS selector
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)))
{
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
// wait 5 second until download started
Thread.Sleep(5000);
// Go to chrome download page
driver.Navigate().GoToUrl("chrome://downloads/");
string oldName = "";
bool downloadcomplete = false;
string cssNames = "downloads-manager /deep/ downloads-item /deep/ [id='name']";
string cssDlProgress = "downloads-manager /deep/ downloads-item /deep/ [class*='show-progress']";
while (!downloadcomplete)
{
var progressElements = driver.FindElements(By.CssSelector(cssDlProgress));
// check until no download progress bar
if (progressElements.Count() == 0)
{
oldName = driver.FindElement(By.CssSelector(cssNames)).Text;
downloadcomplete = true;
}
else
{
// download still in progress, wait.
Thread.Sleep(1000);
}
}
// download complete
// remove downloaded file
driver.FindElement(By.CssSelector("downloads-manager /deep/ downloads-item /deep/ [id='remove']")).Click();
// rename
File.Move(@"C:Temp" + oldName, @"C:Tempnewname.ext");
}
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called insideforloop
. based on name and password file will be downloaded. so how to rename each file after download
– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this pathC:Temp
. But not showing downloaded file inchrome://downloads/
menu
– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and clickid="remove"
to clear download list
– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
|
show 10 more comments
watching directory is not always good, because sometime saved filename is different than filename in URL.
go to chrome download page and loop until all download complete, you can see below how to select special element #shadow-root
with CSS selector
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)))
{
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
// wait 5 second until download started
Thread.Sleep(5000);
// Go to chrome download page
driver.Navigate().GoToUrl("chrome://downloads/");
string oldName = "";
bool downloadcomplete = false;
string cssNames = "downloads-manager /deep/ downloads-item /deep/ [id='name']";
string cssDlProgress = "downloads-manager /deep/ downloads-item /deep/ [class*='show-progress']";
while (!downloadcomplete)
{
var progressElements = driver.FindElements(By.CssSelector(cssDlProgress));
// check until no download progress bar
if (progressElements.Count() == 0)
{
oldName = driver.FindElement(By.CssSelector(cssNames)).Text;
downloadcomplete = true;
}
else
{
// download still in progress, wait.
Thread.Sleep(1000);
}
}
// download complete
// remove downloaded file
driver.FindElement(By.CssSelector("downloads-manager /deep/ downloads-item /deep/ [id='remove']")).Click();
// rename
File.Move(@"C:Temp" + oldName, @"C:Tempnewname.ext");
}
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called insideforloop
. based on name and password file will be downloaded. so how to rename each file after download
– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this pathC:Temp
. But not showing downloaded file inchrome://downloads/
menu
– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and clickid="remove"
to clear download list
– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
|
show 10 more comments
watching directory is not always good, because sometime saved filename is different than filename in URL.
go to chrome download page and loop until all download complete, you can see below how to select special element #shadow-root
with CSS selector
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)))
{
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
// wait 5 second until download started
Thread.Sleep(5000);
// Go to chrome download page
driver.Navigate().GoToUrl("chrome://downloads/");
string oldName = "";
bool downloadcomplete = false;
string cssNames = "downloads-manager /deep/ downloads-item /deep/ [id='name']";
string cssDlProgress = "downloads-manager /deep/ downloads-item /deep/ [class*='show-progress']";
while (!downloadcomplete)
{
var progressElements = driver.FindElements(By.CssSelector(cssDlProgress));
// check until no download progress bar
if (progressElements.Count() == 0)
{
oldName = driver.FindElement(By.CssSelector(cssNames)).Text;
downloadcomplete = true;
}
else
{
// download still in progress, wait.
Thread.Sleep(1000);
}
}
// download complete
// remove downloaded file
driver.FindElement(By.CssSelector("downloads-manager /deep/ downloads-item /deep/ [id='remove']")).Click();
// rename
File.Move(@"C:Temp" + oldName, @"C:Tempnewname.ext");
}
watching directory is not always good, because sometime saved filename is different than filename in URL.
go to chrome download page and loop until all download complete, you can see below how to select special element #shadow-root
with CSS selector
using (var driver = new ChromeDriver(HostingEnvironment.ApplicationPhysicalPath, options)))
{
//driver.findElement(By.xpath("//a/u[contains(text(),'Re-Submit')]")).click();
driver.FindElementById("save").Click();
// wait 5 second until download started
Thread.Sleep(5000);
// Go to chrome download page
driver.Navigate().GoToUrl("chrome://downloads/");
string oldName = "";
bool downloadcomplete = false;
string cssNames = "downloads-manager /deep/ downloads-item /deep/ [id='name']";
string cssDlProgress = "downloads-manager /deep/ downloads-item /deep/ [class*='show-progress']";
while (!downloadcomplete)
{
var progressElements = driver.FindElements(By.CssSelector(cssDlProgress));
// check until no download progress bar
if (progressElements.Count() == 0)
{
oldName = driver.FindElement(By.CssSelector(cssNames)).Text;
downloadcomplete = true;
}
else
{
// download still in progress, wait.
Thread.Sleep(1000);
}
}
// download complete
// remove downloaded file
driver.FindElement(By.CssSelector("downloads-manager /deep/ downloads-item /deep/ [id='remove']")).Click();
// rename
File.Move(@"C:Temp" + oldName, @"C:Tempnewname.ext");
}
edited Nov 26 '18 at 8:32
answered Nov 26 '18 at 7:19
ewwinkewwink
11.8k22239
11.8k22239
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called insideforloop
. based on name and password file will be downloaded. so how to rename each file after download
– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this pathC:Temp
. But not showing downloaded file inchrome://downloads/
menu
– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and clickid="remove"
to clear download list
– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
|
show 10 more comments
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called insideforloop
. based on name and password file will be downloaded. so how to rename each file after download
– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this pathC:Temp
. But not showing downloaded file inchrome://downloads/
menu
– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and clickid="remove"
to clear download list
– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
Thanks @ewwink , I have a problem. suppose this download method called inside for loop. each time file download based on parameters until loop false. so each time your logic calls. how to handle that? I need to rename file one by one. and please tell me how to rename file using your code, I'm confused with that
– Sachith
Nov 26 '18 at 7:36
I updated my code. suppose this method called inside
forloop
. based on name and password file will be downloaded. so how to rename each file after download– Sachith
Nov 26 '18 at 7:42
I updated my code. suppose this method called inside
forloop
. based on name and password file will be downloaded. so how to rename each file after download– Sachith
Nov 26 '18 at 7:42
According to my code files download properly to this path
C:Temp
. But not showing downloaded file in chrome://downloads/
menu– Sachith
Nov 26 '18 at 7:46
According to my code files download properly to this path
C:Temp
. But not showing downloaded file in chrome://downloads/
menu– Sachith
Nov 26 '18 at 7:46
oh I forgot that, call it in your Download method and click
id="remove"
to clear download list– ewwink
Nov 26 '18 at 7:50
oh I forgot that, call it in your Download method and click
id="remove"
to clear download list– ewwink
Nov 26 '18 at 7:50
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
for download directory, try stackoverflow.com/questions/22536907
– ewwink
Nov 26 '18 at 7:51
|
show 10 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475157%2fhow-to-rename-downloaded-file-using-selenium-c-sharp-webdriver%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of python selenium, find out when a download has completed?
– Bishal Gautam
Nov 26 '18 at 5:28
@BishalGautam Please don't mark this as a duplicate. I'm asking for C#, not python. I saw that question too. but it not helped to me
– Sachith
Nov 26 '18 at 5:39
2
@Sachith the idea is the same regardless of language used -- watch the download directory and process the files when they appear.
– No Refunds No Returns
Nov 26 '18 at 5:42
@NoRefundsNoReturns How can I do it. please help me to solve it
– Sachith
Nov 26 '18 at 5:58