I have a trouble getting an element from HTML page - vbscript

what I do is I navigate to a site then I want to find an element called "jobId"
Const PAGE_LOADED = 4
Set objIE = CreateObject("InternetExplorer.Application")
Call objIE.Navigate("http://172.25.25.32:8090/")
objIE.Visible = True
Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(100) : Loop
objIE.Document.all.Username.Value = "ashishgi"
objIE.Document.all.Password.Value = "apac2015#"
If Err.Number <> 0 Then
msgbox "Error: " & err.Description
End If
Call objIE.Document.all.gaia_loginform.submit
Set objIE = Nothing
when I use ViewSource this is the elements I want to use :
input type="submit" name="btnsubmit" value="Login" id="logincaption" class="button" style="color:#565656"

So, you're trying to click submit? Your explanation is difficult to interpret what you want the script to do. Based on the element you displayed on your sample is a submit button. It's been a while since I automated a form but I believe you want something to this effect.
objIE.Document.getElementsById("Username").Value = "ashishgi"
objIE.Document.getElementsById("Password").Value = "apac2015#"
' If you want to use the button by name
objIE.Document.all.item("btnsubmit").click
' If you want to use the button by ID
objIE.Document.getElementsById("logincaption").click
'if that fails, make it an object like this and action the click.
Set Submit = objIE.Document.getElementsById("logincaption")
submit.click

Related

how to copy from a field from a website using vbs

Hi i am new to vbs and I am trying to create a vbs script to enter data into a field on a website and output the text returned to a text file
thats what I created so far:
Dim objIE
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = 1
objIE.navigate "http://translation.babylon.com/english/to-spanish/"
WScript.Sleep 100
Do while objIE.busy
Wscript.sleep 200
Loop
objIE.Document.all.Item("translator_input").Value = "sample text"
WScript.Sleep 100
Do while objIE.busy
Wscript.sleep 200
Loop
Call objIE.Document.all.Item("btnTranslate").Click
Objie.Quit
any ideas how to copy the transelation into a txt. or excel doc.?
Using Chrome's DOM inspector, it appears the translated text is put into this element:
<div id="resltext" name="resultext">
</div>
So you should be able to get the text by just looking for the ID and returning the value of innerText:
strTranslated = objIE.Document.All.Item("resltext").innerText

Asp-classic Vbscript webpage language switcher into variable

For example i have language switcher in index.asp
<ul class="drop-lang_menu">
<li id="LanguageSelected"><img src="img/icons/flags/ru.png" />Rus
<ul>
<li><img src="img/icons/flags/ru.png" />Rus</li>
<li><img src="img/icons/flags/lt.png" />Lit</li>
<li><img src="img/icons/flags/us.png" />Eng</li>
</ul>
</li>
</ul>
and also in this file i have translate function for "Login" button text translate
<%=transl("Login")%>
This function is explained in file function.inc which included in index.asp
<%
Dim Lang
Lang = Document.getElementById("LanguageSelected").innerText
Function transl(TxT as String)
Dim d
d = Application("TranslateList")
If d = "" Then
d = FetchTranslateList(TxT)
Application("TranslateList") = d
End If
transl = d
End Function
Function FetchTranslateList(TxT as String)
Dim rs, fldName, s
Set rs = CreateObject("ADODB.Recordset")
rs.Open "select "+Lang+" from Translations where txt='"+TxT+"'", _
"dsn=name;uid=sa;pwd=;"
s = "<select name=""Translations"">" & vbCrLf
Set fldName = rs.Fields("+Lang+")
Do Until rs.EOF
s = s & " <option>" & fldName _
& "</option>" & vbCrLf
rs.MoveNext
Loop
s = s & "</select>" & vbCrLf
rs.Close
Set rs = Nothing
Set fldName = Nothing
FetchTranslateList = s
End Function
%>
Questions is:
Lang = Document.getElementById("LanguageSelected").innerText seem not working! So how to get selected language value and translate webpage for each user separately ?
Is that is right way to make webpage translation depending on language which user select on the site?
Use asp to change language (document.getElement... is javascript!):
Rus</li>
then, lang = request.querystring("lang")
Other approach is create text files (rus.asp, esp.asp,...) with variables (txt_title="Titulo", txt_button_yes="Si",...) and includes one file or another depending of language. Include files is fast than query to database.
Another thing: is not a good idea create includes with .inc beacuse the code might be visible. Use .asp instead.
Document.getElementById("LanguageSelected").innerText looks like client side Javascript. ASP is server side code, it's executed when the page is served, so you'll need to populate your variable Lang either with a querystring value or a form submission and retrieve it with something like Lang = request("Lang")

How can I strip the element using vbscript and display in message box?

I would like to find the price with 2 year contract and display it in a message box. Sofar I have:
Dim MyPage
Dim Price
Set MyPage=CreateObject("Microsoft.XMLDOM")
MyPage.load("http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723")
Wscript.Sleep 2000
Set Price = MyPage.getElementsByTagName("span")
For Each Elem In Price
MsgBox(Elem.firstChild.nodeValue)
Next
I understand that I am completely wrong, but I don't even know where to start. I love writing simple programs like this, but I just need help getting started. Any ideas will help!
Here a better version, uses the HTMLFile object
Dim HTMLDoc, XML, URL, table
Set HTMLDoc = CreateObject("HTMLFile")
Set XML = CreateObject("MSXML2.XMLHTTP")
URL = "http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723"
With XML
.Open "GET", URL, False
.Send
HTMLDoc.Write .responseText
End With
Set spans = HTMLDoc.getElementsByTagName("span")
for each span in spans
WScript.Echo span.innerHTML
next
'=><SPAN>Set Location</SPAN>
'=>Set Location
'=><SPAN>Submit</SPAN>
'=>Submit
'=>Connect with us
the control you use is for reading XML documents, you need something like this
'Create an xmlhttp object, the string depends on the version that is installed
'on your pc could eg also be "Msxml2.ServerXMLHTTP.5.0"
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", "http://admin:pasword#10.0.0.2/doc/ppp.htm", False
xmlhttp.Send
text=xmlhttp.responseText
wscript.echo text
Set xmlhttp = Nothing
Run a search in your registry for XMLHTTP to get the right string/version for the identifier.
To get the tag from the html you can use the following
text = "blabla <span>this is what i need</span> bla bla<span>second item</span> end"
function getElementsByTagName(sTextToSeachIn, tag)
answer = ""
separator = ""
set oRegExpre = new RegExp
with oRegExpre
.IgnoreCase = true
.Global = true
.MultiLine = True
.Pattern = "<" & tag & ">(.*?)</" & tag & ">"
end with
set oColMatches = oRegExpre.Execute(sTextToSeachIn)
for each match in oColMatches
answer = answer & separator & match.subMatches(0)
separator = "|" 'use something that's not in the spancontents
next
if separator <> "" then
getElementsByTagName = split(answer, separator)
else
getElementsByTagName = array()
end if
end function
for each tag in getElementsByTagName(text, "span")
wscript.echo tag
next
'=>this is what i need
'=>second item
There are better techniques and certainly better languages than vbscript to do this, i suggest to take a look at Ruby which exels in such things.
Alex, in response to your comment about getting a cookie and running a javascript in HTMLFile, here a ruby script i found, hopes it helps you at some point, it reads in a page, passes it to the HTLMFile object and in that DOM executes a remote javascript file. It also gives you an idea of the combined power of activeX and Ruby.
require "win32ole"
$jsxpath_uri = "http://svn.coderepos.org/share/lang/javascript/javascript-xpath/trunk/release/javascript-xpath-latest-cmp.js"
uri, xpath = "http://gist.github.com/gists", "//div[#class='info']/span/a"
http = WIN32OLE.new('MSXML2.XMLHTTP')
http.Open "GET", uri, false
http.Send
text = http.responseText
dom = WIN32OLE.new("htmlfile")
dom.Write(text)
dom.parentWindow.eval(open($jsxpath_uri){|f| f.read })
items = dom.evaluate(xpath, dom, nil, 7, nil)
len = items.snapshotLength
(0...len).each do |i|
item = items.snapshotItem(i)
puts item.innerHTML
end

Update a database record without refreshing a page. ASP VBscript

This requests for help follows a poorly worded request for help earlier (apologies).
I have two asp/vbscript pages. The second page (containing only <% vbscript %> is called when a form on the first page is submitted.
The code on the second page causes an update to a database record and is as follows (the fields are populated from querystring variables.):
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "dsn=xxx;uid=xxx;password=xxx;"
SQLString = "UPDATE dbo_tbl_printing_tempstore SET " & fieldPrefix & "has_text1 = 'YES', " & fieldPrefix & "text = '" & fieldUpdate & "' WHERE id = " & tempid & ""
MyConn.Execute(SQLString)
MyConn.Close
Set MyConn = Nothing
All the form submit does is to cause the database update to happen - nothing else.
The page then response.redirects back to the calling page. This causes a refresh and lot of data on the first page to be lost - this is what I'm trying to avoid.
Can someone please tell me how I can carry out the update without leaving and then refreshing the calling page please? I've been told Ajax can do this but I have no experience at all of using it.
Many thanks
First of all, change your code to use parameters so you will be protected against SQL Injection attack:
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "dsn=xxx;uid=xxx;password=xxx;"
SQLString = "UPDATE dbo_tbl_printing_tempstore SET " & fieldPrefix & "has_text1 = 'YES', " & fieldPrefix & "text = ? WHERE id = ?"
Set MyCommand = Server.CreateObject("ADODB.Command")
Set MyCommand.ActiveConnection = MyConn
MyCommand.CommandType = 1
MyCommand.CommandText = SQLString
MyCommand.Parameters.Append(MyCommand.CreateParameter("#text", 200, 1, 0, fieldUpdate))
MyCommand.Parameters.Append(MyCommand.CreateParameter("#id", 3, 1, 0, tempid))
MyCommand.Execute()
MyConn.Close
Set MyCommand = Nothing
Set MyConn = Nothing
Having this, the next step is adding hidden frame in the first page:
<iframe id="MyFrame" name="MyFrame" style="display:none;"></iframe>
And finally simply add target to your <form> tag like this:
<form action="SecondPage.asp" target="MyFrame">
That's it... now the form will be submitted "inside" the hidden frame, will still trigger the database update and won't cause any refresh.

Call out to script to stop with attribute in wWWHomePage

I'm gettinga n error message in line 8 when I try to call out the script to stop when it finds teh attribute in the Web page: field in AD.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUserDN)
strwWWHomePage = objItem.Get("wWWHomePage")
If wWWHomePage 6 Then
wscript.quit
Else
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
You have:
If wWWHomePage 6 Then
I'm assuming you want it to say:
If wWWHomePage = 6 Then
Since the missing "=" will cause an error, but since that code really doesn't do anything anyway, other than just abort the script, you could simplify your code by only taking action if that value is not set, for example:
If objItem.Get("wWWHomePage") <> 6 Then
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
I'm also assuming "6" is some sort of flag you've set yourself, you might want to use something a little more descriptive like "PPTSTATUS006", or something along those lines.

Resources