Selenium using Python - Storing screenshot in folder with date and timestamp - selenium

Using Selenium with Python, I am trying to take screenshot and saving them in a folder which
has date and timestamp but nothing is getting saved in the folder. This is the code snippet:
Folder creation activity:
path = 'C:\\Users\\name\\Desktop\\ADF'
DateString = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
os.chdir(path)
NewFolder = 'PBI_' + DateString
os.makedirs(NewFolder)
To save the screenshot, I am using:
driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), NewFolder,'Before_Authentication.png'))
Folder name "NewFolder" is getting created successfully but no screenshot is present here.

import datetime
path = 'C:\\Users\\name\\Desktop\\ADF'
DateString = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
os.chdir(path)
NewFolder = 'PBI_' + DateString
os.makedirs(NewFolder)
driver.save_screenshot(NewFolder+'/foo.png')
Seems to create the file just fine just append the file name to the new folder.

Related

How can i get short path from folder directory Vb.Net

i have some path's that i get from folder when i drop in my program but i want to start the path from where the program located.
im putting the program inside the desktop and drag and drop my folder call "Folder" into my program and getting full path and i want get only from where the program is located.
for example:
what i want:
tools\test.exe
tools\test2\test.exe
normal:
C:\Users\xxx\Desktop\Folder\tools\test.exe
C:\Users\xxx\Desktop\Folder\tools\test2\test.exe
my code:
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
For Each Dir As String In IO.Directory.GetDirectories(path)
ListBox1.Items.Add(path)
Next
Next
This should work:
Dim relativePath = fullPath.Substring(Application.StartupPath.Length + 1)
The 1 is added to remove the slash that I don't think is included in Application.StartupPath.
Try below line of code:
Dim relpath=System.IO.Directory.GetCurrentDirectory().Replace(System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName,"")
This should also work:
Dim relPath = fullPath.Substring(Directory.GetCurrentDirectory.Length + 1)

Zip new file in VB: File is being used by another procces

Hi folks I'm trying to zip file from a path to another path using ZipArchive and ZipFile but I can't achieve it.
zipped = "C:\Images\zip\file01.ZIP"
file = "C:\Images\file01.BAK"
Using newFile As ZipArchive = ZipFile.Open(zipped, ZipArchiveMode.Create)
newFile.CreateEntryFromFile(zipped, file, CompressionLevel.Optimal)
End Using
I'm getting the error: "C:\Images\zip\file01.ZIP" File is being used by another proccess
I will appreciate the help
Try archive mode Update instead of Create for a new entry in a zip archive from an existing file
zipped = "C:\Images\zip\file01.ZIP"
file = "C:\Images\file01.BAK"
Using newFile As ZipArchive = ZipFile.Open(zipped, ZipArchiveMode.Update)
newFile.CreateEntryFromFile(zipped, file, CompressionLevel.Optimal)
End Using

Extracting from your resources VB.net

I have a .zip folder in the .exe resources and I have to move it out and then extract it to a folder. Currently I am moving the .zip out with System.IO.File.WriteAllByte and unziping it. Is there anyway to unzip straight from the resources to a folder?
Me.Cursor = Cursors.WaitCursor
'Makes the program look like it's loading.
Dim FileName As FileInfo
Dim Dir_ExtractPath As String = Me.tb_Location.Text
'This is where the FTB folders are located on the drive.
If Not System.IO.Directory.Exists("C:\Temp") Then
System.IO.Directory.CreateDirectory("C:\Temp")
End If
'Make sure there is a temp folder.
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
'This is where the .zip file is moved to.
Dim Dir_FTBTemp As String = Dir_ExtractPath & "\updatetemp"
'This is where the .zip is extracted to.
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
'This moves the .zip file from the resorces to the Temp file.
Dim UnleashedZip As ZipEntry
Using Zip As ZipFile = ZipFile.Read(Dir_Temp)
For Each UnleashedZip In Zip
UnleashedZip.Extract(Dir_FTBTemp, ExtractExistingFileAction.DoNotOverwrite)
Next
End Using
'Extracts the .zip to the temp folder.
So if you're using the Ionic library already, you could pull out your zip file resource as a stream, and plug that stream into Ionic to decompress it. Given a resource of My.Resources.Unleashed, you have two options for getting your zip file into a stream. You can load up a new MemoryStream from the bytes of the resource:
Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Unleashed)
...
End Using
Or you can use the string representation of the name of the resource to pull a stream directly from the assembly:
Dim a As Assembly = Assembly.GetExecutingAssembly()
Using zipFileStream As Stream = a.GetManifestResourceStream("My.Resources.Unleashed")
...
End Using
Assuming you want to extract all the files to the current working directory once you have your stream then you'd do something like this:
Using zip As ZipFile = ZipFile.Read(zipFileStream)
ForEach entry As ZipEntry In zip
entry.Extract();
Next
End Using
Taking pieces from here and there, this works with 3.5 Framework on Windows 7:
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim tmpZip As String = My.Application.Info.DirectoryPath & "\tmpzip.zip"
Using zip As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myProject.myfile.zip")
Dim by(zip.Length) As Byte
zip.Read(by, 0, zip.Length)
My.Computer.FileSystem.WriteAllBytes(tmpZip, by, False)
End Using
'Declare the output folder
Dim output As Object = shObj.NameSpace(("C:\destination"))
'Declare the input zip file saved above
Dim input As Object = shObj.NameSpace((tmpZip)) 'I don't know why it needs to have double parentheses, but it fails without them
output.CopyHere((input.Items), 4)
IO.File.Delete(tmpZip)
shObj = Nothing
Sources: answers here and https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
Since we are using the shell to copy the files, it will ask the user to overwrite them if already exist.

VB.net Return String with Path of folder

My program needs to read an XML file that a software vendor sent me in order to complete a process. The problem is I cannot tell the program where the file is locate!
When i publish the program and install the program it generates a random folder every time it installs
Location the same folder name is always different
C:\Users\Ray\AppData\Local\Apps\2.0\6ZNVVG8V.C6O\0MELQPL9.LCB\lol-..tion_531c8308fa0ff83d_0001.0000_5a2aee0cd0a667c1
I Have figured out how to get that folder to show by doing this
Dim resourcePath As String = _
System.IO.Path.GetFullPath(My.Resources.ResourceManager.BaseName)
Dim rIndex As Integer = resourcePath.LastIndexOf("\")
resourcePath = resourcePath.Substring(0, rIndex)
Dim filePath As String = System.IO.Path.Combine(resourcePath, "Client.xml")
But the program creates a second folder that it puts the XML and the ICOn file in thats randomly generated but in the same directory.
How do I get the program to look in that folder for the xml?
PLEASE HELP ME !
Ray
You could do this:
Dim query = _
From d In System.IO.Directory.GetDirectories(resourcePath)
Let f = New FileInfo(System.IO.Path.Combine(d, "Client.xml"))
Where f.Exists
select f.FullName
Dim filePath = query.FirstOrDefault()
get list of all files by filter
lblPaymentMode.Location = New Point(lblDate.Right - lblPaymentMode.Width, lblPaymentMode.Location.Y)
Dim mFiles() As String = Directory.GetFiles("Path of folder", "*.xml", SearchOption.AllDirectories)
For i As Integer = 0 To mFiles.Count - 1
Debug.Print(mFiles(i)) 'print name and path of of each file
Next

get directory name of directory containing directory

How to get the folder name from the full path of folder?
This is file path,
"c:\projects\roott\wsdlproj\devlop\beta2\text"
Here text is the folder name.
But i want to get the folder containing text, that is beta2
The Path.GetDirectoryName method can be used to return "c:\projects\roott\wsdlproj\devlop\beta2", as shown below:
Dim filePath As String = "c:\projects\roott\wsdlproj\devlop\beta2\text"
Dim directory As String = Path.GetDirectoryName(filePath)
To get just the name of the parent folder, "beta2", you can split the input and take the second last entry, given that the input is indeed accurate:
Dim split As String() = filePath.Split("\")
Dim parentFolder As String = split(split.Length - 2)
Fri 7/09/2012 10:42 AM
io.path.getFileName(filePath) will return the folder name