Need help navigating a page

Started by mathia, July 25, 2014, 09:11:40 AM

Previous topic - Next topic

mathia

I have gotten most of the way though it, but ran into trouble with jquery.  Not a clue where to start. I need to click/navigate to the Management Center link.  I'm attaching the page source.  Any help would be greatly appreciated.


Deana

Please post the code you have so far.
Deana F.
Technical Support
Wilson WindowWare Inc.

mathia

So far, the only code I have is what got me to the page I am trying to navigate.

mathia

Browser = ObjectCreate("InternetExplorer.Application")
browser.visible = @true
url = "http://bvsperformance.bvsinc.com/registrationbank.asp"
browser.navigate(url)
gosub loading
;submitbutton=doc.Body.GetElementsByTagName("submit")
;submitbutton.click()

doc = browser.document
user=doc.Body.GetElementsByTagName("input").Item(0) ;members only
user.value="ID"
user.post
submitbutton=doc.Body.GetElementsByTagName("input").item(1)
submitbutton.click()
gosub loading
submitbutton1=doc.Body.GetElementsByTagName("INPUT").Item(0) ;attention
submitbutton1.click()
gosub loading
user=doc.Body.GetElementsByTagName("input").Item(0) ;username
user.value="username"
pass=doc.Body.GetElementsByTagName("input").Item(1) ;password
pass.value="password"
gosub loading
submitbutton=doc.Body.GetElementsByTagName("INPUT").Item(2)  ;welcome screen
submitbutton.click()
gosub loading
submitbutton=doc.Body.GetElementsByTagName("Input").Item(0)
submitbutton.click()
gosub loading

Deana

The following article looks like an interesting start to executing jQuery from a script: http://www.s-anand.net/blog/automating-internet-explorer-with-jquery/
Deana F.
Technical Support
Wilson WindowWare Inc.

mathia

Thanks. I will check it out to see if I can make heads or tails of this thing.

mathia

I got a little ways into it this morning, but I am not able to get a reference to return.  Everything seems to execute properly, but when I try to use JQuery, there is no object reference so I get an error.  I have little to no experience with UDF's thus far, but had to use one in this instance.  Am I doing something wrong that you can see?  Here is my code:

#DefineFunction addJQuery(jquery,browser,url) 
    document = browser.document
    window = document.parentWindow
    head = document.getElementsByTagName("head");

  •     script = document.createElement("script")
        script.type = "text/javascript"
        script.src = url
        head.appendChild(script)
        return jquery
        timedelay(4)
        ;gosub loading
    #EndFunction
    jquery=""
    url="http://jqueryjs.googlecode.com/files/jquery-1.2.4.js"
    Browser = ObjectCreate("InternetExplorer.Application")
    ie = ObjectCreate("InternetExplorer.Application")
    browser.visible = @true
    ;import win32com.client
    ;ie = win32com.client.Dispatch("InternetExplorer.Application")

    ;# Display IE, so you'll know what's happening
    ;ie.visible = 1

    ;# Go to Google Video
    ;browser.navigate("http://video.google.com/")
    browser.navigate("http://youtube.com/")
    gosub loading

    ; Wait till the page is loaded
    ;from time import sleep
    ;while ie.Busy: sleep(0.2)

    ;# Print the contents
    ;# Watch out for Unicode
    ;print ie.document.body.innertext.encode("utf-8")
    ;def addJQuery(browser,

    ;    while not window.jQuery: sleep(0.1)
    ;   return window.jQuery

    jQuery = addJQuery(jquery,browser,url)

    ; Video title: "McCain's YouTube Problem ..."
    jQuery("title").text(0)

    ; Title link: '/videoplay?docid=1750591377151076231'
    jQuery("#hs_title_link").attr("href")

    ; Duration and date: '3 min - May 18, 2008 - '
    jQuery("#hs_duration_date").text()

    ; Rating: 5.0
    jQuery("#hs_ratings img").length

    ; Number of ratings '(8,288 Ratings) '
    jQuery("#hs_ratings span.Votes").text()

    ; Site: 'Watch this video on youtube.com'
    jQuery("#hs_site").text()

    ; Video description
    jQuery("#hs_description").text()

    :loading
       TimeDelay(4)

       While browser.readystate <> 4
             TimeDelay(0.5)
       EndWhile
    return

stanl

I posted a jquery test back in 2011 (I attached it).  Back then I had IE 9 and it returned an error, but I just tested it with IE 11 and it returns DISPATCH [for the jquery object).

Maybe I am misunderstanding the original post but jquery must be returned as an object and should reference a library of js methods and properties (the script uses Google's).

Again, I haven't messed with jQuery in years, and usually when I see DISPATCH, I rely on Tony or Deana to do the heavy lifting. Another option is to use the F12 Developer's Tools to track the actual jQuery process.

[EDIT]:  I did see the reference to the google library.... guess I was looking for exactly where your script failed. However, if you can get you script to return a jquery object, I would assume it could be parsed as a json array, or maybe some sort of AJAX method to parse it....  Sorry for any confusion from the initial part of this response.

mathia

I looked at your code. I based mine on the same article.  I am stuck at the point of it returning a reference to the jquery object.  Not sure where to go from here.  I thought maybe I had messed up in putting together the UDF.  Haven't done anything with anything like that since college, which was a loooooong time ago.  Since videos.google.com is not the same as it was at the time the article was written, I am not even sure that youtube would return similar results or not.  If you have further guidance to offer, it would be greatly appreciated.

stanl

In looking at the text file you posted, I'm not sure you really need a jquery object. Seems the references are there to support several of the functions in the code. Since the actual site is password protected cannot actually see the display.  But it appears you need to access a menu option then execute jscript code to proceed.

mathia

You are correct. I am trying to access a menu item.  I am automating a process that involves logging into the site, then uploading a file.  I saw the jquery thing in the code of the site and thought that was the way to go to get there.

stanl

Quote from: mathia on July 29, 2014, 06:04:57 AM
You are correct. I am trying to access a menu item.  I am automating a process that involves logging into the site, then uploading a file.  I saw the jquery thing in the code of the site and thought that was the way to go to get there.

OK. From what I see, you need to iterate the pages listitems.  Each has an ID and an Href.  For example,

id='courses24'   has an href \"menuNL/continuingEducationCredit.asp\" 

and I would think you could code the click() event of the listitem which would launch the href.  There may be an example in the tech db associated with this. I can look in my archives after work.

Deana

You might try this based on Stans suggestion of locating the item by its id then click on it:

Code (winbatch) Select
oIE = ObjectCreate("InternetExplorer.Application")
oIE.visible = @true
url = "C:\TEMP\dynamic learning.html"
oIE.navigate(url)

while oIE.readystate <> 4
timedelay(0.5)
endwhile

oElement = oIE.Document.getElementByID("td_perf")

;Click on Element
;oElement.click()
;OR
oEvent = oIE.document.createEvent("HTMLEvents")
oEvent.initEvent("click", @TRUE, @TRUE)
oElement.dispatchEvent(oEvent)

while oIE.readystate <> 4
        timedelay(0.5)
endwhile
exit
Deana F.
Technical Support
Wilson WindowWare Inc.

mathia

Tried this with slight changes to fit into my existing script as you cannot get to the particualr page I am on without logging in and going through the preceding pages.

;oIE = ObjectCreate("InternetExplorer.Application")
;oIE.visible = @true
;url = "C:\TEMP\dynamic learning.html"
;oIE.navigate(url)

while browser.readystate <> 4
        timedelay(0.5)
endwhile

oElement = doc.getElementByID("td_perf")

;Click on Element
;oElement.click()
;OR
oEvent = doc.createEvent("HTMLEvents")
oEvent.initEvent("click", @TRUE, @TRUE)
oElement.dispatchEvent(oEvent)

while browser.readystate <> 4
        timedelay(0.5)
endwhile

oEvent returns a null.

mathia

Probably speaking too soon, but I think this method will work on the next page, but not the one I am currently stuck on.   I am trying to click on the Management Center item.  Is it different from a regular item you would click on?

Deana

Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Quote from: mathia on July 29, 2014, 11:03:24 AM
Probably speaking too soon, but I think this method will work on the next page, but not the one I am currently stuck on.   I am trying to click on the Management Center item.  Is it different from a regular item you would click on?

For that you need to run -  constructMenu("perf") which then creates the listItems you can select from. I believe you can execute it from the document object.  Not sure, but something like.....

oIE.document.parentWindow.execScript(""javascript:constructMenu("perf") ;"")

or

oIE.document.parentWindow.execScript('javascript:constructMenu("perf") ;')

mathia

Quote from: Deana on July 29, 2014, 11:28:55 AM
Did you try oElement.click()?

You, my dear, are a genius.
I saw that commented out and didn't know whether to try it or not.  I did and made it though the next 2 pages. I am now working to see how to select an item that I think is in a frame. I am looking at the source for the page, but am not seeing the text that is displayed for the link.  I've even tried Stan's ie analyze tool with an htm file. 

Deana

Quote from: mathia on July 29, 2014, 01:49:14 PM
Quote from: Deana on July 29, 2014, 11:28:55 AM
Did you try oElement.click()?

You, my dear, are a genius.
I saw that commented out and didn't know whether to try it or not.  I did and made it though the next 2 pages. I am now working to see how to select an item that I think is in a frame. I am looking at the source for the page, but am not seeing the text that is displayed for the link.  I've even tried Stan's ie analyze tool with an htm file.

Excellent. Glad to hear that worked.
HINT: F12 option in your browser can be helpful in identifying webpage elements.
Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Quote from: mathia on July 29, 2014, 01:49:14 PM
I've even tried Stan's ie analyze tool with an htm file.

I think when I first posted that, iframes and divs were not part of the options. You never mentioned what version of IE you were using which might explain why the object.click() worked and Deana's other suggestion didn't. Out of curiosity, what if you manually navigate all the way to a desired page, copy/paste the url into notepad, then modify your base script to login to the site with credentials then navigate to that url - would it work. If so, you might be able to shortcut your navigation on the site.

mathia

My first approach was just to look for links in the code of the site as I figured that was the easiest way to get there.  I found out very quickly it wasn't going to be that easy.  Even once you are a little deeper into the site, there are no link, just functions that build the next page or menu.  Well, as far as I can see, that's how it is.  Currently, I am on IE 8, but that will probably change very soon.  From what i've read in my quest to complete this task, I will probably be revisiting this again once browser upgrades are pushed out.

stanl

Quote from: mathia on July 30, 2014, 06:25:31 AM
Currently, I am on IE 8

Funny, a lot of corporations are still on 8, and use chrome for other browsing. Just to clarify what I was talking about in terms of navigation, in my last job we had to work with a protected site that was all Jquery, used json to activate hidden divs, and made typical page navigation near impossible (using things like document.readystate were inaccurate, as you couldn't tell when a div or iframe was actually built, and previous data was stored as a _VIEWSTATE)

However, once I played around and determined where I needed to be, the following constants proved invaluable..


navOpenInNewWindow=1
navNoHistory=2
navNoReadFromCache=4
navNoWriteToCache=8
navAllowAutosearch=16
navBrowserBar=32
navHyperlink=64
navEnforceRestricted=128
navNewWindowsManaged=256
navUntrustedForDownload=512
navTrustedForActiveX=1024
navOpenInNewTab=2048
navOpenInBackgroundTab=4096
navKeepWordWheelText=8192
navVirtualTab=16384
navBlockRedirectsXDomain=32768
navOpenNewForegroundTab=65536


and I used IE's navigate2() rather than navigate()


oIE.Navigate2(::url=cURL,Flags=navOpenInNewTab)

or

oIE.Navigate2(::url=cURL,Flags=navNoReadFromCache)


so my script would have a number of url entries, many just the base url with a call to a .asp page but I was able to handle them without relying on the cache or opening in a new tab.

mathia

Well, I'm pretty new to all this web stuff, which is why I'm stumbling through it.  I am currently trying to click a link that is contained within an iFrame.  Can't get the code, so I can't post it.  I can return a reference to the iFrame itself, but can't seem to get to the onclick item I'm trying to get to within the iFrame.  I'm not even sure that's what I'm supposed to be looking at. 

Deana

F12 web developer tools built into many browsers will be your friend. ;)
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

Quote from: stanl on July 30, 2014, 06:15:13 AM
Quote from: mathia on July 29, 2014, 01:49:14 PM
I've even tried Stan's ie analyze tool with an htm file.

I think when I first posted that, iframes and divs were not part of the options. You never mentioned what version of IE you were using which might explain why the object.click() worked and Deana's other suggestion didn't. Out of curiosity, what if you manually navigate all the way to a desired page, copy/paste the url into notepad, then modify your base script to login to the site with credentials then navigate to that url - would it work. If so, you might be able to shortcut your navigation on the site.

Here is a function that can be used to click using either method if available:

Code (winbatch) Select
#DefineFunction udfIEClick( objIE, objElement )
    objEvent = objIE.document.createEvent( "HTMLEvents" )
    If objEvent == "" Then
      objElement.Click()
    Else
      objEvent.initEvent("click", @TRUE, @TRUE)
      objElement.dispatchEvent(objEvent)
    EndIf
    Return 1
#EndFunction

Deana F.
Technical Support
Wilson WindowWare Inc.

mathia

I've googled and read and tried and tried.  Finally found the links collection.  I can get a list of all of the anchor tags in the section I need.  None of them have names, though.  I can click every link in the collection using a foreach loop and was able to count down through the code to figure out which one I need to click.  After that, I can simply reference it using the item property of the links collection.  This all seems a bit kludgey to me. Is there a way to identify an anchor tag that doesn't have a name property? It only has text that identifies it as far as I can see.   I've tried all kinds of properties, but they all seem to return a NULL.  Except for the href property which returns the same thing for each item.  Hope some of that makes sense.

mathia

When a new window opens, how do you get a reference to the new browser object?

td

One approach is to use the "Shell.Application" object.  I think there is a better way but can't recall it at the moment.

The "Shell.Application" approach can be found on this page.  You will need to scroll down a bit to find it.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsrch.web+~+TechHome
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mathia

If I get a window handle using this method, will I still be able to use the IE object model?

td

The UDS in the tech db returns an IE application object which means you have access to the document object.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mathia

Excellent.  By the way, that link only takes me to a seach page.

td

Yes the changes to the tech db require a different approach to getting links to search results.  You can find the article and others by executing a search on the mentioned progid.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mathia

I'm assuming you're referring to this piece of code:

#definefunction BrowserGetObject()
oShellApp=Objectaccess("Shell.Application",1)
oWindows=oShellApp.Windows()
count=oWindows.Count   
obrowser=oWindows.Item(count-1)
objectclose(oShellApp)
return obrowser
#endfunction

When I add this to my existing code, it returns an object reference, but it is a reference to the window that spawned the second browser instance.  I have verified this, I think, by getting the title of the window.  If it helps, here is my code leading up to all of this

link=iframe.contentwindow.document.GetElementsByTagName('div').id('s29')
link.length
count=0
foreach link in elementlist
   test2=link.id
   if test2=='s29' then index=count
   count=count+1
next
test3=elementlist.item(index).getelementsbytagname('input')
len=test3.length
test4=test3.item(0)
test4.click()  ;This line opens a new browser window
gosub loading ;wait 4 seconds for page to load
test7=browserGetObject()

title=test7.document.title
oelement= test7.document.getElementBytagname("input") ;returns 0 elements
test=oelement.length ;error, presumably because there aren't any elements

td

Actually, I was refer to the article entitled "Working With Web Pages" - script Q10.WBT.  It is the very first article link when you search on "shell.application" sans quotes. Note this script returns an 'objWindows' object but that is a meaningless distinction in this case.




"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade