Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

Click Java Button in URL with Excel VBA

Trying to achive downloading table from company website. I can download first page. However, cannot jump to second page.

HTML CODE for Page Number

<a href="javascript:__doPostBack('ctl00$View$gv','Page$1')">1</a>

HTML CODE [![HTML CODE FOR TABLE][1]][1]

page numbers are inside table and increasing one by one. at the first time when page one is active link href is not visible and shows as

<span>1</span>

I use below code to click page however I cannot succeded.

Set doc = ie.document

i = 0
For Each link In doc.Links
'doing downloading stuff here
i = i + 1
link.innerText = "javascript:__doPostBack('ctl00$View$gv','Page$" & i
link.Click
Next

When I check the page also there is a javascript function.

Javasript CODE

//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>

after first page downloaded, macro click irrelevant page links even never click same page for each time.

Extra Question

also is there any way to get href values instead of innertext on below code

<a href="Link">User Name</a>

Thanks

question from:https://stackoverflow.com/questions/65843820/click-java-button-in-url-with-excel-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Open any page by parameter of the url:
Look if you can open any page directly by a parameter of the url for the page number like this: https://yourUrl.com?page=2

Then the walk through all pages is very easy. The only thing you must check at first is the number of the pages or a html code that only is in the page code when you try to open a page that is not available.

How to get href
You can't click innertext. That is only a string. You ask for a way to get the href and that is the right thought. If you want get the href of the first a-tag you can use this:

'Part of your code to open the page
'...
Dim nodeFirstLink as Object
Set nodeFirstLink = doc.getElementsByTagName("a")(0)
Debug.Print nodeFirstLink.href
'More of your code
'...

Here is an example how to change the href
But I don't know if this works also with JS links:

Sub ChangeHref()

  Dim htmlDoc As Object
  Dim nodeFirstLink As Object
  
  'Set a short HTML Document for this example
  Set htmlDoc = CreateObject("HtmlFile")
  htmlDoc.body.innerHTML = "<a href='https://amazon.com'>Amazon</a>"
  
  Set nodeFirstLink = htmlDoc.getElementsByTagName("a")(0) 'Get the first Link
  Debug.Print nodeFirstLink.outerhtml                      'The HTML of the first link in the html document
  Debug.Print nodeFirstLink.href                           'Only the href of the first link in the html document
  
  nodeFirstLink.href = "https://ebay.com" 'Changing the href in the first link
  Debug.Print nodeFirstLink.outerhtml     'The innertext is still Amazon
  Debug.Print nodeFirstLink.href          'The href is the new one
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...