How to change the location? - python-3.x

I'm trying to automate image uploading to Instagram using Selenium in Python. I'm successful till opening the fileDialogue but I'm not able to change the directory to where the image is located. It returns an error that ToolbarWindow32 can't be detected by AutoIt.
My code:
ActionChains(browser).move_to_element(browser.find_element_by_xpath(
"/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]/div/button")).click().perform()
handle = f"[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "ToolbarWindow32", photopath) # This line give me the Error
autoit.control_set_text(handle, "Edit1", photopath)
autoit.control_click(handle, "Button1")

Take a look how this is done in _WD_SelectFiles with: https://github.com/Danp2/au3WebDriver/blob/master/wd_helper.au3
You should be able to do the same directly with python+selenium without using AutoIt.
Also take a look on:
https://github.com/Danp2/au3WebDriver/blob/master/wd_demo.au3
There is example how directly in AutoIt it is possible to do the same with WebDriver UDF without opening any FileOpenDialog :
Func DemoUpload()
; REMARK This example uses PNG files created in DemoWindows
; navigate to "file storing" website
_WD_Navigate($sSession, "https://www.htmlquick.com/reference/tags/input-file.html")
; select single file
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//section[#id='examples']//input[#name='uploadedfile']", #ScriptDir & "\Screen1.png")
; select two files at once
_WD_SelectFiles($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[#name='uploadedfiles[]']", #ScriptDir & "\Screen1.png" & #LF & #ScriptDir & "\Screen2.png")
; accept/start uploading
Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//p[contains(text(),'Upload files:')]//input[2]")
_WD_ElementAction($sSession, $sElement, 'click')
EndFunc ;==>DemoUpload

Related

Nginx - Lua Unable to save image file

My lua code is something like this:
local savefiletarget = "/usr/local/openresty/nginx/webfolder/img/uploadedimg.jpg".
local fileToSave = io.open(savefiletarget,"w+b")
if not fileToSave then
ngx.log(ngx.NOTICE,'failed to save file : '..savefiletarget);
ngx.say('{"filename" : "'..filenametosave..'","status" : 0 ,"message":"failed to open file"}')
return
end
Log shows the error:
failed to save file : /usr/local/openresty/nginx/webfolder/img/uploadedimg.jpg
I'm using Linux server, do I require permissions to save the image?
Or is there anything I missed?

not allowing me to copy file do to files permission

I have made a program that changes the background picture of my desktop every user defined seconds and there is no problems with that part. However I do have a problem with changing the picture of the log in screen I have set up my computer correctly (a windows 7 computer) to change the background picture (including editing the registry to be able to change the picture more then once).
I currently changing my login picture manually by moving the picture to a folder I created C:\windows\system32\oobe\info\backgrounds. I've chosen to make the task automated using python. I will delete the existing image, copy the new image to the folder and then rename the image to backgroundDefault.jpg and repeat every user defined seconds.
To copy, rename, and delete these files using the os module I've tested these steps in my cmd and it works.
Now what seems to be the problem?
Well I'm able to find the folder using os.path.exists however I'm unable to copy, delete or rename anything because the program doesn't have permission.
It is also worth noting that I have already tried to give my user permission to write in the folder and I already tried to give the python programme administrator access not only the python program but Py.exe and th pyw.exe that sit in the wondows folder
Is there a way to give the program permission or is there another way of changing the folder that I need to move the files to? Or even is there a different snipit of script I could use to achieve a login background change?
def path_writer_bg():
folders_path_bg = input("Please type the folders path of the login background here, then press enter"
"\n>")
if os.path.exists(folders_path_bg):
open("your_path_bg.txt", "w").write(folders_path_bg)
read_folder_path_bg = open("your_path_bg.txt", "r").read()
if os.path.exists("task_bg.txt"):
open("task_bg.txt", "w").write("dir " + read_folder_path_bg + " /s /b >listed_bg.txt")
file_read_task_bg = open("task_bg.txt", "r").readline()
else:
open("task_bg.txt", "w").write("dir " + read_folder_path_bg + " /s /b >listed_bg.txt")
file_read_task_bg = open("task_bg.txt", "r").readline()
os.popen(file_read_task_bg)
else:
input("invalid input. press enter to retry \n")
path_writer_bg()
if os.path.exists("your_path_bg.txt"):
read_folder_path_e_bg = open("your_path_bg.txt", "r").read()
open("task_bg.txt", "w").write("dir " + read_folder_path_e_bg + " /s /b >listed_bg.txt")
file_read_task_e_bg = open("task_bg.txt", "r").readline()
os.popen(file_read_task_e_bg)
else:
path_writer_bg()
def everything_bg():
with open("listed_bg.txt") as file_bg:
num_lines_bg = sum(1 for line_bg in open("listed_bg.txt"))
for num_bg, line_bg in enumerate(file_bg, 1):
rand_line_bg = random.randrange(num_lines_bg - 1)
lines_bg = open("listed_bg.txt", "r").readlines()
open('temp_bg.txt', 'w').writelines(lines_bg[rand_line_bg])
wallpaper_bg()
def wallpaper_bg():
path_bg = open("temp_bg.txt", "r").readline()
if os.path.exists("C:\\Windows\\System32\\oobe\\info\\backgrounds\\backgroundDefault.jpg"):
os.popen("del C:\\Windows\\System32\\oobe\\info\\backgrounds\\backgroundDefault.jpg")
else:
pass
if os.popen("copy /y" + path_bg + " C:\\Windows\\System32\\oobe\\info\\backgrounds"):
os.popen("dir C:\\Windows\\System32\\oobe\\info\\backgrounds /s /b >renamer.txt")
else:
pass
if os.path.exists("renamer.txt"):
rename = open("renamer.txt", "r").readline()
else:
pass
os.popen("rename " + rename + "backgroundDefault.jpg")
wallpaper_bg()
exit()
time.sleep(10)
everything()
Maybe it is because that specific file can't be changed with python due to Microsoft not allowing software to change any file in system32.

PyQt QFileEditor default suffix

I have looked out through bunch of code but this peace of code doesn't work as expected for me:
export_dialog = QtGui.QFileDialog()
export_dialog.setWindowTitle('Export')
export_dialog.setDirectory(EXPORT_DIR)
export_dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
export_dialog.setNameFilter('INI files (*.ini)')
export_dialog.setDefaultSuffix('ini')
export_file, _ = export_dialog.getSaveFileName()
print(export_file)
I'm saving my file without extension, counting on that my above configurations will set it properly, but it doesn't work. There is no extension added.
Any suggestions?
Thanks
export_dialog = QtGui.QFileDialog()
export_dialog.setWindowTitle('Export')
export_dialog.setDirectory(EXPORT_DIR)
export_dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
export_dialog.setNameFilter('INI files (*.ini)')
export_dialog.setDefaultSuffix('ini')
if export_dialog.exec_() == QtGui.QFileDialog.Accepted:
print(export_dialog.selectedFiles()[0])
This code will return a full file path with selected filter also.

Corrupted Excel File & 7zip

I have a problem with a corrupted excel file. So far I have used 7zip to open it as an archive and extract most of the data. But some important sheets cannot be extracted.
Using the l command of 7zip I get the following output :
7z.exe l -slt "C:\Users\corrupted1.xlsm" xl/worksheets/sheet3.xml
Output:
Listing archive: C:\Users\corrupted1.xlsm
--
Path = C:\Users\corrupted1.xlsm
Type = zip
Physical Size = 11931916
----------
Path = xl\worksheets\sheet3.xml
Folder = -
Size = 57217
Packed Size = 12375
Modified = 1980-01-01 00:00:00
Created =
Accessed =
Attributes = .....
Encrypted = -
Comment =
CRC = 553C3C52
Method = Deflate
Host OS = FAT
Version = 20
However when trying to extract it (or test it for that matter) I get :
7z.exe t -slt "C:\Users\corrupted1.xlsm" xl/worksheets/sheet3.xml
Output:
Processing archive: C:\Users\corrupted1.xlsm
Testing xl\worksheets\sheet3.xml Unsupported Method
Sub items Errors: 1
The method listed above says Deflate, which is the same for all the worksheets.
Is there anything I can do? What kind of corruption is this? Is it the CRC? Can I ignore it somehow or something?
Please help!
Edit:
The following is the error when trying to extract or edit the xml file through 7zip:
Edit 2:
Tried with WinZip as well, getting :
Extracting to "C:\Users\axpavl\AppData\Local\Temp\wzf0b9\"
Use Path: yes Overlay Files: yes
Extracting xl\worksheets\sheet2.xml
Unable to find the local header for xl\worksheets\sheet2.xml.
Severe Error: Cannot find a local header.
This might help:
https://superuser.com/questions/145479/excel-edit-the-xml-inside-an-xlsx-file
and this on too: http://www.techrepublic.com/blog/tr-dojo/recover-data-from-a-damaged-office-file-with-the-help-of-7-zip/

How to put multiple documents into Berkeley-DB XML container?

I have a directory with a bunch of XML documents and want to put all of them into a container.
In other words, I need to do something like this:
dbxml> putDocument tests/*.xml
I have written a GUI program to do that but the host server does not have X-windows installed, so must be in command line.
I do a similar thing when reloading certain XML docs into my current application DB. It helps if all of the files sharing a common naming convention. In python you would could use the following script to add doc001.xml to doc009.xml:
from bsddb3.db import *
from dbxml import *
#Load source files 001 - 009
sourceDir = 'C:/directory-containing-xml-docs'
fileRange = range(1,10)
for x in fileRange:
mycontainer = mymgr.openContainer("myDB.dbxml")
xmlucontext = mymgr.createUpdateContext()
xmlinput = mymgr.createLocalFileInputStream(sourceDir + "doc00" + str(x) + ".xml")
mycontainer.putDocument("doc00" + str(x) + ".xml", xmlinput, xmlucontext)
print 'Added: ' + str(x)
del mycontainer
print '1 - 9 Added'
Hope that helps
You could have a shell script write the list of XML files to another file and then call dbxml_load_container with the -f option.
Ended up using a script that lists files and puts everything into the DB.

Resources