i have a problem uploading files with selenium and python. Tha main problem is the textbox where the file path is selected is disabled, so, when i put the absolute path in send_keys, this do nothing.
How can handle the dialog window with JS or another method (In my job, i can't install PyAutoIt or any framework)
The html code is this:
<div class="form-inline actions-toolbar">
<input disabled="disabled" class="input-xxxlarge" id="uploadFile" style="background: rgb(255, 255, 255); margin-left: 10px;" placeholder=""> --> This is the textBox
<div class="fileUpload btn btn-primary">
<span>Explorar</span>
<input name="fileUpload" tabindex="1" class="upload" id="fileUpload" onchange="showPath(this);" type="file" size="50" accept=".txt"> --> This is the button "Search"
</div>
<input name="batchPaymentFilePath" id="batchPaymentFilePath" type="hidden" value="">
<button disabled="disabled" class="btn" id="uploadButton" onclick="validNavigation = true;myFunction();"><i class="icon-upload-alt"></i> Cargar</button> --> Button to load File
</div>
The python code is this:
filePath = os.path.abspath('C:\\file\\path\\file_to_upload.txt')
driver.find_element_by_xpath('//*[#type="file"]').send_keys(filePath)
When i run the script, the upload file dialog window appears just showing the desktop, not the absolute file path.
Thanks guys!
Thanks #sowjanya-r-bhat this is my update of the code that you share with me:
driver.execute_script('document.querySelector("#uploadFile").removeAttribute("disabled");document.querySelector("#uploadFile").setAttribute("enabled", true);')
#Enable the file text box
upload = driver.find_element_by_xpath('//*[#id="uploadFile"]')
#Send the file path
upload.send_keys("C:\\file\\path\\file_to_upload.txt")
#Enable Load Button
driver.execute_script('document.querySelector("#uploadButton").removeAttribute("disabled");document.querySelector("#uploadButton").setAttribute("enabled", true);')
I have to enable the load button, because depends of the file selected in the upload file windows.
Thanks again!
Related
Html of the form:
<form action="" method="post" enctype="multipart/form-data"><input type="hidden" name="csrfmiddlewaretoken" value="nTfw60C2FSwLfGTvOlO6FITRnz4yq2HObLpcDw1enoqaT9JsD4ztaDJpCpBVBpS7">
<div class="file-field input-field">
<div class="btn">
<input type="file" name="uploaded" id="id_uploaded">
<span>Choose file</span>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" id="id_path">
</div>
</div>
<div class="col s12">
<button class="btn-flat" id="id_submit" type="submit">Submit</button>
</div>
</form>
The problem is that a click on the file text input opens a file dialog and MaterializeCSS doesn't accept pasting a file path string. Also, only the filename without the full path is shown in the text input after a user selects a file in the dialog.
When the following code runs then the form invalidates with my no file provided error:
text_input = webdriver.find_element_by_id("id_path")
text_input.clear()
text_input.send_keys(path_to_file)
webdriver.find_element_by_id("id_submit").click()
Any suggestions on how to solve this problem and provide a file in tests? Any workaround could be an acceptable answer as I just want to test the functionality that takes place after submitting a file.
I need to upload file. I have "Choose file" form. I click "Choose file" button, select file in window, after that uploading starts.
Here is form.
<form id="fileupload" method="POST" enctype="multipart/form-data" data-confirm="true">
<div class="uploadForm">
<div class="browseUploadLeft">
<i class="icon icon-discontinued-upload"></i>
<h3>Browse and choose</h3>
<p>files from your computer</p>
</div>
<div class="browseUploadRight">
<p class="browseInfo is-hidden">Need help? See <span>Upload Rules</span></p>
<span class="button button-pink buttonFullWidth">choose files to upload
<input type="file" id="fileUploadField" name="Filedata" multiple class="filemultiple video" />
</span>
<p class="sizeNote">Maximum file size limit is 10GB</p>
</div>
</div>
<input type="hidden" name="userId" value="8996317" />
<input type="hidden" name="videoId" class='be_video_id' id='be_video_id' value="" />
<input type="hidden" id="_token" name="_token" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJodHRwOlwvXC93d3cueW91cG9ybi5jb20lIiwic3ViIjoiODk5NjMxNyIsImF1ZCI6Imh0dHA6XC9cL3d3dy55b3Vwb3JuLmNvbSUiLCJpYXQiOjE1MDkzNTI5MjEsImV4cCI6MTUwOTM4ODkyMX0.DSMPckUG3ZcL6Zrbn1WWrLgLzJ_tdw3TOZ2hnb2z60qSWMMpfZghJnrliSkwAQVRNjl6H-VKDCZBrlKvGwO0WQ" />
</form>
I want do this using python + selenium. I have code.
browser = webdriver.Firefox()
browser.get("url")
time.sleep(10)
browser.find_element_by_id("fileUploadField").click()
file = browser.find_element_by_id("fileUploadField")
file.send_keys("1.m4")
file.submit()
But I have error during execution the code.
File "yp.py", line 40, in <module>
file.submit()
......
Message: Element is no longer attached to the DOM
How can I send file to this form in correct way? How to start upload file?
First install win32com.client. To install win32com.client type in cmd
pip install pypiwin32
Now after click on upload, add below code to pass address of file which need to be uploaded.
browser = webdriver.Firefox()
browser.get("url")
time.sleep(10)
browser.find_element_by_id("fileUploadField").click()
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("D:\\FileLocation\\1.m4")
shell.Sendkeys("{ENTER}")
Note: In case you get any issue while installing win32com.client then check whether you have installed python for 32 bit os, if not then make sure you have installed python for 32 bit even if your os is 64 bit os. There won't be any problem in installation and win32com.client will work there
Try setting full path:
driver.find_element_by_id("fileUploadField").send_keys(os.getcwd()+"/1.m4")
The problem looks simple but everywhere I search it I get results for uploading a file, whereas my use case is, that based on a few params, I in my Handler decide the relevant file and upload it as a link in my View. I am using tornado for this. e.g :
<div class="form-group"> <!-- Date input -->
<label for="actDateFrom" class="control-label">Date</label>
<input ng-model="data.actDateFrom" class="form-control" name="actDateFrom" placeholder="MM/DD/YYY" type="text"/>
</div>
<div class="form-group"> <!-- Date input -->
<label for="actDateTo" class="control-label">Date</label>
<input ng-model="data.actDateTo" class="form-control" name="actDateTo" placeholder="MM/DD/YYY" type="text"/>
</div>
<div class="form-group"> <!-- Submit button -->
<button type="submit" class="btn btn-default" data-ng-disabled="form.$invalid" data-ng-click="sendActRequest()" >Go Fetch !</button>
</div>
Based on the inputs above, I have a handler, that should basically filter a csv file and get that file showing up as a link .
All I am stuck at is the code for uploading that subset file as a link in my view, I can manage the rest. Please not that the file is lying somewhere on the server itself.
Thanks in Advance!
Lol that was so easy, I think the funda is that only a static file can be served, I just put it in my some web/static/files path which is visible to the server and posted a href link to it, something like below:
<div>
Activations Logs for the time period.
</div>
Please dont downvote, actually I am new to Web Dev :P Thanks !
I tried to click on it directly and tried switching frame but it doesn't automate it when i manually click on it it works an d open a window popup to upload file
but using selenium it doesn't:
<div>
<input id="id" name="name" type="file" class="class" size="31" style="display: none; background-color: grey;" width="95" height="31">
<embed src="some_url" quality="high" width="95" height="31" id="fnameUploader" class="fileUploaderBtn" name="fnameUploader" type="application/x-shockwave-flash">
</div>
I'm unable to upload and save an image.
The following is the piece of code that facilitates file upload using the UI. At first, only the upload button is visible and once an image is selected from the uploader the save button is visible.
<td colspan="1" class="actions">
<input type="button" class="button" name="logo" id="logo-f-upload" value="Upload Image"/>
<input type="file" id="logo-upload" accept="image/*" name="logo-upload" style="display:none" />
<input type="submit" value="click to save" name="submit_logo_now" class="main submit_dm logo-save-focused" style="display: none"/>
</br>
</td>
I tried driver.find_element_by_id("logo-f-upload").send_keys(os.getcwd()+"/image_test.png")
and also
driver.find_element_by_id("logo-upload").send_keys(os.getcwd()+"/image_test.png")
but it doesn't throw any error at this stage but on the next where it says "element is not visible ..." ie. the save button. On the UI, when simulating this, the file uploader doesn't open but the file_upload button value changes to the path of the image and then breaks.
I believe this is the answer which is merely a JS hack: So the problem is that the logo-upload input has style set to display:none and due to this, selenium can't find the element. The solution that works for me is using javascript to set the style to display:block, after which using send_keys(img_path) works fine.
dr.execute_script("document.getElementById('logo-upload').setAttribute('Style','display:block')")