I am trying to develop and wpf application with selenium for learning purpouse, but i am facing someting strange. I want to use EFPlus to modify an Excel row at each itteration, on some computers works just fine, but also on another computers i get :
Nu s-a putut modifica EXCELUL cu SENT pentru id 3164092 + System.InvalidOperationException: Error saving file C:WPFSEleniumExcelFoldertest.xlsx ---> System.InvalidOperationException: Part does not exist.
at OfficeOpenXml.Packaging.ZipPackage.GetPart(Uri partUri)
at OfficeOpenXml.Packaging.ZipPackage.DeletePart(Uri Uri)
at OfficeOpenXml.ExcelWorksheet.SaveDrawings()
at OfficeOpenXml.ExcelWorksheet.Save()
at OfficeOpenXml.ExcelWorkbook.Save()
at OfficeOpenXml.ExcelPackage.Save()
--- End of inner exception stack trace ---
at OfficeOpenXml.ExcelPackage.Save()
at WPFSElenium.MainWindow.<WaitForConfirmation>d__10.MoveNext() in C:WPFSEleniumWPFSEleniumWPFSEleniumMainWindow.xaml.cs:line 478
the code:
confirmEvent
private async void WaitForConfirmation()
{
confirmEvent.WaitOne();
string chatId = "xxxxxxxxx";
int controBlocareCont = 0;
int idNegasiteConcomitent = 0;
Application.Current.Dispatcher.Invoke(() =>
{
if (Application.Current.MainWindow != null)
{
Application.Current.MainWindow.WindowState = WindowState.Minimized;
}
});
Thread.Sleep(GenerateRandomInterval());
string directoryPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "ExcelFolder");
if (Directory.Exists(directoryPath))
{
string[] excelFiles = Directory.GetFiles(directoryPath, "*.xlsx");
if (excelFiles.Length > 0)
{
string filePath = excelFiles[0];
// Add full permissions to the Excel file
FileInfo fileInfo = new FileInfo(filePath);
FileSecurity fileSecurity = fileInfo.GetAccessControl();
fileSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, AccessControlType.Allow));
fileInfo.SetAccessControl(fileSecurity);
using (var package = new ExcelPackage(new FileInfo(filePath)))
{
ExcelWorkbook workbook = package.Workbook;
if (workbook != null)
{
ExcelWorksheet worksheet = workbook.Worksheets[0];
int rowCount = worksheet.Dimension.End.Row;
worksheet.Protection.IsProtected = false;
for (int row = 2; row <= rowCount; row++)
{
if (worksheet.Cells[row, 4].Value == null)
{
string id = worksheet.Cells[row, 1].Value?.ToString();
if (id != null && (id.Length != 2 && id.Length != 1))
{
Thread.Sleep(GenerateRandomInterval());
try
{
divElement.Text = "Test";
string NumePersoana = divElement.Text;
if (NumePersoana == worksheet.Cells[row, 2].Value.ToString())
{
divElement.Click();
ExcelWorksheet worksheetMesaje = workbook.Worksheets[1];
int rowCountMesaje = worksheetMesaje.Dimension.End.Row;
Random random = new Random();
int randomRow = random.Next(2, rowCountMesaje + 1);
string mesajAleatoriu = worksheetMesaje.Cells[randomRow, 1].Value.ToString();
IWebElement textArea = null;
try
{
textArea = driver.FindElement(By.XPath("));
Thread.Sleep(GenerateRandomInterval());
textArea.SendKeys(mesajAleatoriu);
Thread.Sleep(GenerateRandomInterval());
textArea.SendKeys(Keys.Enter);
Thread.Sleep(IntervalSendMessage);
worksheet.Cells[row, 4].Value = "Sent";
package.Save();
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
}
catch (NoSuchElementException)
{
worksheet.Cells[row, 4].Value = "Blocked";
package.Save();
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
continue;
}
catch (Exception ex)
{
string messageSentErr = $"ATENTIE: Nu s-a putut modifica EXCELUL cu SENT pentru id {id} + {ex}";
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
await _botSender.SendMessageAsync(chatId, messageSentErr);
}
}
else
{
worksheet.Cells[row, 4].Value = "Not Sent";
package.Save();
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
controBlocareCont++;
if (controBlocareCont == 10)
{
string messageBlocked = "ATENTIE: Posibil sa aveti contul BLOCAT. nVa rugam repornit BOT-ul cu un nou cont";
await _botSender.SendMessageAsync(chatId, messageBlocked);
}
}
}
catch (NoSuchElementException)
{
worksheet.Cells[row, 4].Value = "Not Sent";
package.Save();
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
idNegasiteConcomitent++;
if (idNegasiteConcomitent % 10 == 0)
{
string messageElementNotFound = $"ATENTIE: {idNegasiteConcomitent} Id-uri nu a fost identificate la rand pe FACEBOOK nVerificati corectitudinea fisierului EXCEL";
await _botSender.SendMessageAsync(chatId, messageElementNotFound);
}
}
catch (Exception ex)
{
string messageNotSentErr = $"ATENTIE: Nu s-a putut modifica EXCELUL cu NOT SENT pentru id {id} + {ex}";
InputID.SendKeys(Keys.Control + "a");
InputID.SendKeys(Keys.Delete);
await _botSender.SendMessageAsync(chatId, messageNotSentErr);
}
}
}
}
package.Save();
package.Dispose();
string message = "ATENTIE: Au fost procesate toate PERSOANELE din EXCEL-ul furnizat. nVa rugam repornit BOT-ul cu un nou set de date";
await _botSender.SendMessageAsync(chatId, message);
}
}
}
}
}
the problem appear at package.Save();
i tried to do package.Dispose()
after every excel modification , but at the second itteration i get another erroar that says the worksheet is disposed
SergiuBrk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.