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')")
Related
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!
I want to click a three way toggle using selenium with python.The html element Ids are dynamic. So I have tried with an XPath where class contains a specific text! But I seeing 'element not found'/'element not visible' whole day!
I've tried with below line of code but no help.
browser.find_element_by_xpath("//*[contains(#class,'switch switch-three toggle ios') and contains(text(),'Available')]").click()
Here is the HTML code of the page and I want to click on - 'Available'
<label class="switch switch-three toggle ios" id="radio8526" onclick="" style="float: left; width: 300px; height:15px; margin-right: 20px;margin-left: 20px;">
<input id="available8526" value="available" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">
<label for="available8526" onclick="">Available</label>
<input id="unavailable8526" value="unavailable" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">
<label for="unavailable8526" onclick="">Unavailable</label>
<input id="archived8526" value="archived" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio" checked="">
<label for="archived8526" onclick="">Finalised</label>
<a class="slide-button"></a>
</label>
From w3c documentation You can use this to solve your problem
browser.find_element_by_css_selector('input[id^="available"]').click()
You can just use the value attribute, e.g.
input[value='available']
You might need to add a wait for clickable to make sure the page has loaded. See this link for more info and some examples.
Selenium is able to find these elements and send_text to them, but right after the text is written it is automatically deleted. Am I calling .send_text() wrong, or am I finding the wrong element to interact with?
This is a popup frame dialog, but I can't seem to focus on the frame. I'm not even sure I need to as selenium still interacts with the text fields.
Here is the javascript:
<div class="control-group">
<label class="control-label">Patient ID</label>
<div class="controls">
<input data-ng-model="subject.Code" data-custom-error="Patient ID already exists in this center" class="ng-valid ng-dirty" data-original-title="" type="text">
<i class="required-field" style="top:inherit">*</i>
</div>
</div>
Here is one method I have been using:
patientID = 'testID'
driver.find_element_by_xpath("//input[#data-ng-model='subject.Code']").send_keys(patientID)
also,
driver.find_element_by_css_selector(".controls > input[data-ng-model='subject.Code']").send_keys(patientID)
As I said, both find the correct element and send text but the text is immediately deleted. Any ideas, I'm stuck.
I'm using firefox, but the same happens in chrome.
Have you checked if there is any javascript code in the page listening to changes to the input field and deleting its contents?
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 have to perform two actions based on radio button selection, either download or view a document
<form method="post" action="{{ url_for('page_after_submit') }}">
<p> Your resume </p>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked> Download document </label>
</div>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2"> View document </label>
</div>
<input type="submit" />
</form>
My page_after_submit has this code...
#app.route(local.URL_PREFIX + '/page_after_submit/', methods=['POST'])
def after_submit():
if 'option1' == request.form['optionsRadios']:
return/redirect ("download from this url")
if 'option2' == request.form['optionsRadios']:
return/redirect ("view in this iframe")
return (Url_for('go back to submit page if you are here')
I know my form can only have one action which is '/page_after_submit/', what code (HTML or Python in flask) I need to complete rest of my actions ??? I tried to put the iframe tags with complete download file address in the redirect for option2 but doesn't work. I also want this iframe to pop up not open a new browser window. Plus for the download, don't know what to do specially different operating system may have different path for download directory.
My goal is to not have any javascript as well, don't know if it's possible or not. Thanks in advance.
You need to craft a different response depending on how they want to see the data.
Download File
If they choose option 1, you need to set the headers and response to allow the browser to trigger a file download. Here's how you can do something like that.
View File in Iframe
If they choose option 2, you need to return an HTML response which loads the file. This can be done in an Iframe if you'd like, but it's not necessary. Here's one possible way to do that, but many others exist.