Selenium File upload - xpath

How to automate Uploading a file using selenium.
How to give file Path ??
My TextBox is Readonly. I cant type the file path directly in the textbox.
Also, how to stop the selinum server until that file completely uploaded.??
My File upload field is a invisible field. And i found its code using firebug add on.
Before adding a file code is like this.
<input id="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState" type="hidden" name="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState" autocomplete="off" value="{'isEnabled':'true','uploadedFiles':[]}">
And after adding a file(doc file). The code changed to
<input id="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState" type="hidden" name="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState" autocomplete="off" value="{'isEnabled':'true','uploadedFiles':[{"fileInfo":{"FileName":"scope.docx","ContentType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document","ContentLength":12887},"metaData":"/wEFsAF7IlRlbXBGaWxlTmFtZSI6ImZyeWd1NGNqLmt1YSIsIkFzeW5jVXBsb2FkVHlwZU5hbWUiOiJUZWxlcmlrLldlYi5VSS5VcGxvYWRlZEZpbGVJbmZvLCBUZWxlcmlrLldlYi5VSSwgVmVyc2lvbj0yMDExLjEuNTE5LjM1LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPTEyMWZhZTc4MTY1YmEzZDQiffOraDjiYXPavAAMYOUAVVhGEKk8"}]}">
What is the Xpath here?
I tried with xpath id="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState". The code which i used is
selenium.type("id="ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState","c:\\docfile1.doc");
But its not working.
Help Me..

The XPath expression for this input would be //input[#id='ctl00_ContentPlaceHolder1_AsyncfileUpload_ClientState'].
However, I fear this won't work since Selenium usually refuses to work with invisible elements. Also, hidden <inputs> are usually just containers for pre-filled data or containers for script-validated-and-edited data.
You should be looking for a <input type='file' /> if there's some, or maybe a javascript handling the click on the enclosing element (but, frankly, that's usually not the case - the scripts tend to act on edit of the input, not on the click on them).
If you can't find it, post some more code. The best thing would be a SSCCE, so take the source of the page and make it naked, strip everything unnecessary for us from it. We love code. And we love anything that's naked.
And about the wait for the upload to be complete: There's no such default thing. If the file is sent during a usual form upload (by clicking the Submit button), then the browser will wait. If it is uploaded immediately, you'll have to wait smartly. Realize what changes after a successful upload, then wait for that element/message to appear. With Selenium 2 (WebDriver), this can be done very easily.

You can use
selenium.type("xpath of text box","path of your file")
OR for IDE
command=type
target=xpath_of_text_box
value=Path_of_your_file
example:
selenium.type("id=cvfile", "D:\\Automation\\resume.doc");

Related

AlpineJS submit form on button click posts the old value instead of the new one

I'm new to alpineJS, so this might be a silly question. I try to increase an input field by 1, and submit the new value (preferably with a small delay).
I currently have:
<input type='button' value='+' class='qtyplus plus' field='quantity' x-on:click="qty++ && $event.target.form && $event.target.form.dispatchEvent(new Event('submit'));" />
This increases the value of the input form by 1, and submits the form. The problem is that the submitted value is the old value, and not the new one.
Any help with this is much appreciated!
The && operator in JavaScript is used to test conditions for true/false. You should instead use ; to put multiple commands in a single click. JavaScript does some unexpected conversions of values to true/false values. I'm surprised it works even partially. The mysteries of JavaScript! 😆
MDN has some good info on the && operator - called Logical AND
Here's your code sample with the change. I also made a couple of other changes using Alpine JS features:
debounce might be useful for you as it tells Alpine JS to wait until the user has stopped clicking before it executes the click code - which means your server won't have to process so many requests.
x-ref can be used instead of $event.target.form (not useful for you I think, but might be useful for others reading this question)
Form elements have a submit() method you can use. MDN has good info on this as well.
<form x-ref="myform">
<input
type='button'
value='+'
class='qtyplus plus'
field='quantity'
#click.debounce="qty++; refs.myform.submit()" />
</form>
After some more debugging I found the solution by using two on-click events, one that works directly and one that is delayed. This solved the issue for me.
<input type='button' value='-' class='qtyminus minus' field='quantity'
x-on:click="qty--;" #click.debounce.2000ms="$event.target.form.submit();"/>

Paste image URL to html text box instead of 'browsing for file'

How can I convert the following into an editable text box, with a submit button to carry out the code below?
<input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" />
The use case is that an image URL is in the memory, and just needs to be pasted for submission, instead of choosing a local file. (since the OS grabs the remote file, makes a local copy of it then passes this on)
I'm essentially tweaking this element from a nice jcrop tutorial:
http://www.script-tutorials.com/html5-image-uploader-with-jcrop/
Many thanks!

Input text inside a link tag in IE8

Is there a way to make input text inside a link tag works well in IE8? I cannot place the caret inside nor select the text within it.
<input type="text">
I think the reason why I'm trying to do this is not important here, just consider I have no choice of make it work under an <a> tag. I only have control over what's inside the <a> tag.
As a solution, I was thinking about some JQuery DOM manipulation when in IE8 mode but there must be a easier/cleaner way of fixing this "bug".
Thanks
I think this is due to the input and link tag display properties, while an input is display:inline-block; your link is display:inline;. Try to play with these properties and z-index if it's not working.
Hovever, i think jQuery solution is better, and simpler, except if this is your only usage of jQuery on your page.
HTML :
<input type="text" />
jQuery script
jQuery(document).ready(function(){
$("#myInputLink").click(function(){
// .trigger('focus') or .focus(), but the first one is better on my own
$(this).next('input[type="text"]').trigger('focus');
});
});
Have a nice day.

A way around Element cannot be scrolled into view - Watir-webdriver with Ruby

So, we have the following code in our page:
<div class="toggle-wrapper">
<input id="HasRegistration_true" class="registration_required toggle" type="radio" value="True" name="HasRegistration" data-val-required="The HasRegistration field is required." data-val="true">
<label for="HasRegistration_true" class="">On</label>
<input id="HasRegistration_false" class="registration_required toggle" type="radio" value="False" name="HasRegistration" checked="checked">
<label class="checked" for="HasRegistration_false">Off</label>
</div>
These are 2 radio buttons. 'On' and 'Off'. 'Off' is the default value.
Using Watir-webdriver and Ruby, we want to select the 'On' radio button. We do so like this:
browser.radio(:id => "HasRegistration_true").set
But in doing so, we get the following error:
`WebElement.clickElement': Element cannot be scrolled into view:[object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)
We know Selenium 2 scrolls the page to the element, so trying to scroll down is useless.
We are always using the latest releases of watir-webdriver and ruby.
We can't change the HTML of the page since we're QA engineers.
Here are two solutions that have worked for me:
There is a Ruby gem called watir-scroll that can work.
Then
require 'watir-scroll'
browser.scroll.to browser.radio(:id, "HasRegistration_true")
If you don't want to add a gem, my co-worker suggested something that somewhat surprisingly (to me) had the same effect as the above code:
browser.radio(:id, "HasRegistration_true").focus
browser.radio(:id, "HasRegistration_true").set
In my code, ".focus" scrolled straight to the element that was previously not visible. It may work for you as well.
First of all try locating the element using XPATH:
browser.element(:xpath, "//input[#id='HasRegistration_true']").click
or
alternatively if it is a hidden element you are trying to locate then you are better off using CSS. Download firebug add-on for firefox and copy the CSS path of your element.
It should be something like:
browser.element(:css => "the CSS path you have copied from Firebug").click
One of the 2 should do it for you!!
Best of luck!
You could manipulate the html on the fly by executing some javascript to make the radio element settable. To execute javascript on a page, do something like:
#browser.execute_script("your javascript here")
I used something like the following javascript to strip the class out of a label tag which moved it out of the way of the input tag I was attempting to act on for a Chrome specific problem I had.
execute_script("$(\"label.classname\").removeClass(\"classname inline\")")
If the element is contained within a form and a div (Wrap) class I found that I had to do the following to click the 'No' radio button on the "https://quote.comparethemarket.com/Motor/Motor/AboutYourVehicle.aspx?" page:
div_list = #browser.form(:action => "AboutYourVehicle.aspx?ton_t=CTMMO&prdcls=PC&rqstyp=newmotorquote&AFFCLIE=CM01").div(:class => "inputWrap").divs(:class => "custom-radio")
And then:
div_list[1].click
Hope this solves your issue too :-)
I'm not using watir but I have the same error as you "...could not be scrolled into view ...". I tried to use watir just to solve it and didn't work for me. Then, I use an ActionBuilder (move_to with click) and the error disappeared.
My line to avoid the error is:
#driver.action.move_to(*webelement*).click.perform
I hope it will be useful for you

Form Submit differences in Firefox and Google Chrome (ColdFusion)

A few days ago i've made a little Coldfusion 9 script, but somehow after submitting the form the query won't run in Firefox. Everything works well in Google Chrome, but not in Firefox.
I've tried to Google it, but i can't find anything that relates to this problem. If something is unclear for you after reading this - just ask and i'll try to explain it better to you.
The script can be found on: http://pastebin.com/Bic28B3L
Kind regards,
You can leave your script as it is and replace
<cfif IsDefined('form.submit')>
with
<cfif CGI.REQUEST_METHOD IS "POST">
to check if the form was submitted.
My guess would be that FF and Chrome are handling the form element generated by this line differently:
<cfinput type='image' src='http://linehotel.org/c_images/bot_buy.png' name='submit' value='Koop deze badge nu'>
it looks like your code is checking that form.submit exists, so I would look at the source that this tag generates.
The other thing to try would be to install and run Fiddler and use it capture the submissions made by Chrome and FF and compare the two, particularly look at the WebForms tab on the request section. There'll be a difference there if it's browser-related.
The culprit is two-folds:
<cfinput type='image'...>
And
<cfif IsDefined('form.submit')>
Coldfusion will generate the HTML for your CFINPUT like this:
<input type="image"...
And Firefox will generate a form post like this:
submit.x: mouseclick x coords.
submit.y: mouseclick y coords.
Firefox will not return the name of the image in the form post. It will only return the X and Y values of the image map. What you will probably have to do is replace the <CFINPUT> TAG with an HTML <INPUT type="submit"> button and use CSS to apply an image to it. You may even consider checking for some other form variable in lieu of the submit button.
Looking at your code, you may also want to look at the cfqueryparam tag to help protect against sql injections and you may want to do your calculations outside the cfquery rather than in it... for example:
<cfquery name = "pay" datasource = "#DSN#">
UPDATE users
SET bots=#user.bots#-1
WHERE id=#user.id#
</cfquery>
may be better like this:
<Cfset xBots=user.bots-1>
<cfquery name = "pay" datasource = "#DSN#">
UPDATE users
SET bots=<cfqueryparam cfsqltype="cf_sql_integer" value="#xBots#">
WHERE id=<cfqueryparam cfsqltype="cf_sql_integer" value="#user.id#">
</cfquery

Resources