I need to automate the action of hitting 'Enter' key on keyboard after entering a text in a text field.
I tried #browser.send_keys :enter
but that does not do the action. also have tried #browser.text_field(:name => 'q').send_keys :enter or #browser.text_field(:name => 'q').focus and then send_keys. But has not helped.
#browser.send_keys("{ENTER}")
does not help this too, this actually types ("{ENTER}")
Please let me know other ways of doing?
browser.send_keys :enter should do the job. Please provide link to the page, link to a similar page, or relevant HTML.
try browse.send_keys :return
found it here http://watirmelon.com/about/
Given the HTML:
<input type="text" size="30" class="searchText" dojoattachpoint="_searchTextAP" id="xwt_widget_uishell_Header17_2_search_searchTextAP" dojoattachevent="onfocus:_onFocus_searchTextAP,onblur:_onBlur_searchTextAP, onkeyup:_onKeyUp_searchTextAP">
It looks like the element is listening for these events (via dojo): onfocus, onblur, onKeyUp
To trigger these events, you'll need to use something like this:
browser.text_field(:id => /.*searchTextAP$/).focus
browser.element(:id => 'someOtherElement').focus
browser.text_field(:id => /.*searchTextAP$/).fire_event "onkeyup"
If you want to submit the form, you probably need to click the submit button (since you mentioned hitting :enter):
browser.button(:type => 'submit').click
If you really wanted to just send the :enter key, one of the other answers should work.
Related
I'm writing a VBA code to login into a webpage and load some information i have in an excel worksheet.
I'm new in Selenium. I already got the login part right, but now i need to click in an element and i keep getting errors.
I need to click in the Company 2 button.
This is what i've got so far:
bot.FindElementByXPath("//input[#value=""Company 1""]").Click
Outputs NoSuchElementError
bot.FindElementByXPath("//input[#value=""Company 2""]").Click
Outputs ElementNotVisible
I don't know what i'm doing wrong, i think that the first input being hidden has something to do. Hope anyone can help me.
Might help you to know you can also use ByCss in most circumstances, in which case you can use:
bot.FindElementByCss("input[value='Company 1']").Click
That is nice and short.
The CSS selector is input[value='Company 1']. This says find element with input tag having attribute value with value of 'Company 1'.
XPath might be incorrect. Please try the following syntax:
FindElementByXPath("//input[#value='Company 1']")
First of all, use CSS selectors whenever possible. They are much easier to handle.
Now if you are using CSS selectors try to find the second button using something like
input[value="Company 2"]
For more info on this selector, look at https://www.w3schools.com/cssref/sel_attribute_value.asp
You can use any xpath, in first look I found your xpath is incorrect, try this:
//input[#type='button'][#value='Company 2']
//input[#type='button'&& #value='Company 2']
//input[#role='button'][#value='Company 2']
You can also use findelements() to store are all buttons and using if else you can extract the company 2 button
As per the HTML you have shared to invoke click() on the desired elements you can use the following solution:
To click on the element with text as Company 1:
bot.FindElementByXPath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all' and #value='Company 1']").Click
To click on the element with text as Company 2:
bot.FindElementByXPath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all' and #value='Company 2']").Click
Have you tried right-clicking the HTML in inspect and going to Copy>Copy XPath? That might give you something different. Maybe the buttons are created from Javascript and so the WebDriver can't actually see them?
Or try
Company_1 = bot.find_element_by_xpath("//input[#value='Company 1']")
Company_1.click()
Company_2 = bot.find_element_by_xpath("//input[#value='Company 2']")
Company_2.click()
And change the syntax with the ' ' quotes like someone else mentioned.
I have the following code on the page:
<div class="col-md-5 col-lg-3">
<div class="react-selectize bootstrap3 root-node simple-select open">
<div class="react-selectize-control">
<div class="react-selectize-placeholder">testing!</div>
<div class="react-selectize-search-field-and-selected-values">
<input class="resizable-input" style="width: 4px;" type="input">
If I physically select the input field and then run the following using watir-classic:
BROWSER.div(:class => 'col-md-5 col-lg-3')
.div(:class => 'react-selectize-search-field-and-selected-values')
.text_field(:class => 'resizable-input')
.send_keys('Magg')
it will enter Magg into the field, but if I don't manually select the field I can't seem to get watir to enter the text.
I've tried the following:
BROWSER.div(:class => 'col-md-5 col-lg-3')
.div(:class => 'react-selectize-search-field-and-selected-values')
.text_field(:class => 'resizable-input')
with
.set('Magg')
.fire_event('onfocus')
.fire_event('mousedown')
.fire_event('mouseup')
using
.parent.text_field(:class => 'resizable-input')
.parent.input(:class => 'resizable-input').set('Magg')
.input(:class => 'resizable-input').set('Magg')
.input(:class => 'resizable-input').send_keys('Magg')
I've been working with the developer using the control and neither of us can figure out how to give the control focus so that the send_keys works without manually selecting the control. Any guidance would be greatly appreciated.
First - obligatory warning that Watir Classic is deprecated and you should be using Watir WebDriver. (which may fix this issue)
Second - technically: div(:class => 'col-md-5 col-lg-3') should be div(:css => '.col-md-5.col-lg-3') since class should be singular.
Have you tried just doing element.fire_event(:click) before element.set?
Be aware that your HTML has an invalid 'type' value for the input tag. Per W3.org the type of 'input' is not a valid type choice for an input tag.
That is likely what is causing the method you expect to work (such as .set) to fail. It also means that all the watir methods to return the more specific sub-types of input (e.g. .text_field) will not work to select that tag.
Watir defines the specific input related methods (such as .set, .unset) according to the specific sub-type of input tag, and has methods to select those specific input types (such as .button). Normally you'd select an input tag using the method for it's sub-type, but because that value is not valid, that is not an option in this case, and you are forced to use .input. But as we just discussed, the input object in watir does not have methods like .set defined. So your only fallback is to use .sendkeys to send keystrokes to the element.
If it was me I'd write up a bug on that HTML, that it is not using a proper value for 'type' per the W3 spec.
Fixing the HTML to be valid will likely cause it to work far better with Watir or any other automation tool, especially those based on webdriver.
I am trying to find a way to get more than one characters pasted on html text field (input type=text), using onkeydown or onkeypress, I am NOT interested in onkeyup(it works), so please do not suggest me this solution. Also I am not interested in Jquery, I do not like to use it. I need solution to work with all browsers.
I am able to do that if you type one character, by taking the character of the event, but till now I am unable to get group of characters come from the paste (ctrl+V) or by mouse.
You can look at Facebook search menu that shows auto complete results, it works on events: onkeydown and onkeypress and you notice that you get the result before you release your finger, try to paste something (more than one character) and you get the result before releasing finger, how? Onkeypress and onkeydown do not show first thing you paste or type because they happen before the text being added to text field.
If you want to understand what I want more, please check my code below:
<input type="text" id="targetTextField" onkeydown="CapturePastedString(this.id)" />
<script>
function CapturePastedString(id){
var targetTextField=document.getElementById(id);
// below I need to capture the pasted string like:
var pasted_string= function(){.....}
targetTextField.value=pasted_string;
}
</script>
Any help would be so thankful, because since long time I am trying but cannot find solution yet.
Try the onkeydown handler it works.You can get the keyCode from there and then the key.
<input type="text" value="" onkeydown="alert(String.fromCharCode(event.keyCode));"/>
Note that since all characters on the keyboard are capital you would also get capital letter. The above code would display the key pressed
Using Watir I have been able to isolate this button element:
<BUTTON id=authButton class=commandButton onmousedown="$('newOrder:hiddenAuth').click();">Authorize Payment</BUTTON>
Here is my watir code:
$browser.div(:id, "rSide:j_id750_body").table(:index, 0) [1] [0] .button(:index, 0).click
I was able to see I'm on the <button> by using watir's "flash" method to highlight the button. For some reason I am unable to actually click the button. I think it might have something to do with the onmousedown code but I really don't know. Any help would be greatly appreciated.
The button is wired up to do some specific action when it see's the onmousedown event, so if .click is not working, the next thing to try is firing that specific event, instead of using the .click method.
If that doesn't work try forcing the javascript to execute the newOrderLhiddenAuth script.
oh and FYI for your developer, they may want to get into the habit of using lower case for their HTML tags
"the World Wide Web Consortium (W3C) recommends lowercase in HTML 4,
and demands lowercase tags in XHTML."
I am new to Watir automation testing and would like to get some help for the drop down.On our website we have a state drop down where you enter the first letter of the state (in my example C for California) and it narrows it down to the all states starting with C. Once you have the list you need to click on the correct state. But I am having difficulties selecting the correct state.
(Below is the html from our website:
<div class="x-form-field-wrap x-trigger-wrap-focus" id="ext-gen202" style="width: 166px;">
<input type="hidden" id="entityStateCode" name="entityStateCode" value="">
<input type="text" id="ext-comp-1005" autocomplete="off" size="24" class=" x-form-text x-form-field x-form-focus">
I used the following to automate the scenario but none of these are giving me what i am looking for:
#browser.text_field(:id,"ext-comp-1005").value=("CA")
#browser.text_field(:id,"ext-comp-1005").set("CA")
#browser.text_field(:id=> "ext-comp-1055",:index => 5).set "CA"
I really appreciate that if you can point me to the right direction.
Thanks
I ran into a similar situation before. In my situation there was a TABLE inside the DIV which had a separate row for each item in the dynamic drop down. So, if that's the case for you then you would need to use something like this to access the items:
#browser.text_field(:id,"ext-comp-1055").set "C"
table = #browser.div(:id, "ext-gen336").table(:index, 1)
puts "First entry value: #{table[1][1].text}"
table[2][1].click # second entry
Try printing out the HTML for the DIV at runtime to see the specifics of what you need to interact with if that doesn't work.
You did not tell what the problem is, this is not enough:
none of these are giving me what i am
looking for
This should enter CA into text field:
browser.text_field(:id => "ext-comp-1005").set("CA")
If it is entering text into wrong text field, change :id => "ext-comp-1005".
If it is entering text into the correct text field, but list of states does not appear, you probably have to fire some javascript event. Take a look at How to find out which JavaScript events fired? question.