To call a VBScript function on click of Enter key? - vbscript

I have created a VBScript function OnRefreshList() which is called on the click of a button "Refresh List".
Sub OnRefreshList ()
Initdatatable(false)
if ReadFilters() = false then
msgbox "It is not possible to refresh the whole orders list. Please enter more filters"
exit sub
end if
End Sub
The "Refresh List" button is defined as
<td class="button cmd" valign="center" nowrap id="cmdRefresh" onclick="OnRefreshList()" title="Refresh the order list">Refresh List</td>
This function is working fine when I am clicking on the button.
Now I want to call this function when I press the Enter key from the key board.
For this I tried to change the following piece of code but it didnt worked for me.
<td class="button cmd" valign="center" nowrap id="cmdRefresh" onKeydown="vbscript: if (event.keyCode==13) then OnRefreshList()" onclick="OnRefreshList()" title="Refresh the order list">Refresh List</td>
Please help me if anyone has an answer to it.

Your onKeydown event does not have a correct VBScript statement in it. It should be:
onKeydown="vbscript: if event.keyCode=13 then OnRefreshList()"
Notice the single = sign and the lack of parenthesis (they are not mandatory). If you are more comfortable with javascript: You can mix VBScript and Javascript:
onKeydown="javascript: if (event.keyCode==13) OnRefreshList(); //this will work too!"

Related

vbscript click on button/image

I am creating a vbscript to automatically fill in a webpage. However, I am unable to figure out click on the final submit but as there is no id or tag.
<A onclick="submitForm('DefaultFormName',1,
{'wklEvent':'RR0','serverValidate':'1',event:'respond'});return false;"
href="#"><IMG border=0 alt=Approve
src="/OA_HTML/cabo/images/cache/en/bApprMsEb-6.gif" width=63 align=absMiddle
height=18></A>
Edit: I was able to get it working using the code below
Dim ele As Object
For Each ele In ie.Document.getElementsByTagName("a")
If InStr(ele.innerHTML, "bAdd_-X_1-7.gif") > 0 Then
ele.FireEvent ("onclick")
End If
Next ele

How to click on repeated button on a page?

I have a table layout like this:
<tbody>
<tr>
<td> 1 </td>
<td> <button> Click me </button>
</tr>
<tr>
<td> 2 </td>
<td> <button> Click me </button>
</tr>
....
</tbody>
I only know the value of the first td (1,2, etc). Is there anyway to click on the appropriate second td (the button) while only knowing the first td? For example, can I dynamically get 2 and know to click on the second "Click me" button instead of the first one?
yes there is way:
Basically, the buttons you want to click are siblings to the td elements you already know of.
Please try using locating an element via xpath sibling to locate those buttons:
//td[contains(text(),'1')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '1', then find its immediate sibling.
//td[contains(text(),'2')]/following-sibling::* //this reads, first locate an element of td type whose text() attribute contains '2', then find its immediate sibling.
You need an XPath:
twoClickMeButton = driver.find_element(:xpath,"//td[text()='2']/../td/button")

A Confirmation Box On Exit

I want to have hta code such that:
When the user clicks on the "Close" button, it pops up a confirmation box.
If the user confirms "Yes", then it will quit otherwise it keeps running.
Thanks in advance.
In HTML, you need a close button, it can be an input with button type (but in fact, any part of an HTML page can get a 'onclick' property, like a in a table)
<input type="button" value="Close" onclick='F_closeConfirm'>
then the VB code should contain a function F_closeConfirm with something like this
Public Function F_closeConfirm()
'one way to ask
If MsgBox("Are you sure you want to quit?", vbYesNo, "Confirmation") = vbYes Then
'another way
iAnswer = MsgBox("Like, really quit?", vbYesNo, "Confirmation")
If iAnswer = vbYes Then
'method to close parent windows or tab, can differ but it's another topic
Window.Parent.Close
End If
End If
End Function

Calling multiple functions from single onClick in VBScript

I have created a VBScript function that allows users to install a network printer by clicking a button. This works great, but there is no feedback after clicking the button. I'm trying to add an alert after the button is clicked, but I'm having trouble with the syntax.
Here is the functioning onClick function for installing a printer:
<script type="text/vbscript">
function AddP(pName)
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.AddWindowsPrinterConnection pName
end function
</script>
<td><input name="Button1" type="button" value="Add"></td>
If you want to give the user some indication something is happening while the printer is being added you could change the button state.
<script type="text/vbscript">
function AddP(pName)
Dim allButton1s
Set allButton1s = document.getElementsByName("Button1")
allButton1s(0).value = "Please wait..."
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.AddWindowsPrinterConnection pName
MsgBox "Printer Added"
allButton1s(0).value = "Add"
end function
</script>
Simplest solution: Add a colon to your declaration which acts as a statement separator. This will allow you to call an additional function.
<a href="#" language="vbscript" onclick="AddP('\\PrinterName': msgbox 'Printer added')">
Just add this line after your WshNetwork.AddWindowsPrinterConnection line
MsgBox "Printer Added"

How to press/click the button using Selenium if the button does not have the Id?

I have 2 buttons Cancel and Next button on the same page but it has only one id (see the below code). I wanted to press Next but every time it is identifying the cancel button only not Next button. How to resolve this issue?
<td align="center">
<input type="button" id="cancelButton" value="Cancel" title="cancel" class="Submit_Button" style="background-color: rgb(0, 0, 160);">
<input type="submit" value="Next" title="next" class="Submit_Button">
</td>
Use xpath selector (here's quick tutorial) instead of id:
#python:
from selenium.webdriver import Firefox
YOUR_PAGE_URL = 'http://mypage.com/'
NEXT_BUTTON_XPATH = '//input[#type="submit" and #title="next"]'
browser = Firefox()
browser.get(YOUR_PAGE_URL)
button = browser.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()
Or, if you use "vanilla" Selenium, just use same xpath selector instead of button id:
NEXT_BUTTON_XPATH = '//input[#type="submit" and #title="next"]'
selenium.click(NEXT_BUTTON_XPATH)
In Selenium IDE you can do:
Command | clickAndWait
Target | //input[#value='Next' and #title='next']
It should work fine.
use the text and value attributes instead of the id
driver.findElementByXpath("//input[#value='cancel'][#title='cancel']").click();
similarly for Next.
For Next button you can use xpath or cssSelector as below:
xpath for Next button: //input[#value='Next']
cssPath for Next button: input[value=Next]
You don't need to use only identifier as elements locators. You can use a few ways to find an element. Read this article and choose the best for you.
You can use xpath for for identifying that element.
You can achieve this by using cssSelector
// Use of List web elements:
String cssSelectorOfLoginButton="input[type='button'][id='login']";
//****Add cssSelector of your 1st webelement
//List<WebElement> button
=driver.findElements(By.cssSelector(cssSelectorOfLoginButton));
button.get(0).click();
I hope this work for you

Resources