Click Java Button in URL with Excel VBA - excel

Trying to achive downloading table from company website. I can download first page. However, cannot jump to second page.
HTML CODE for Page Number
1
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
User Name
Thanks

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

Related

InternetExplorer - delete specific element from IE.Document

I am using VBA to automate the pulling of reports from a structured web page.
I have the following code block which changes the dropdown element for each element in an array"
With IE
For Each itemName In itemNames()
i = i + 1
Module2.check_and_wait_for_item ("cboDistrict")
Set Dropdown = .document.getElementsByName("cboDistrict")(0)
Dropdown.Value = itemValues(i)
Dropdown.FireEvent ("onchange")
check_and_wait_for_item ("cmdRptPreview")
Set btnPreview = .document.getElementsByName("cmdRptPreview")(0)
btnPreview.Click
check_and_wait_for_item ("BtnExport")
Set HTMLDoc = .document
write_file_to_dir HTMLDoc, "C:\Users\user\Documents\____thing\", itemName & ".html"
.GoBack
Next itemName
End With
The problem arises with IE.GoBack. I have a function that checks if JavaScript has finished executing by looping while a particular element is not present on the page. "Going back" puts the element back on the page even though the JavaScript event has not finished running.
This is why I want to delete the old "cmdRptPreview" element before I have my function check if JavaScript event has finished running.
Is there a way to do so? Appreciate the help.
The solution was to remove the Node containing the element being checked by JavaScript waiter function.
After .GoBack:
check_and_wait_for_item ("cmdRptPreview")
.document.getElementsByName("cmdRptPreview")(0).ParentNode.RemoveChild .document.getElementsByName("cmdRptPreview")(0)

Error entering data into web page with Excel

I want to enter data into a web page field.
There are 2 data entry fields on the web page.
I entered data in the first section.
However, I cannot enter data in the other field.
Information you need to review the site :
Site : http://splan.byethost7.com/mesaj_yaz.php?fno=1&kip=yeni
user :kurucu password :a11111
I entered the data in the "BAŞLIK" field.
However I am unable to write data to the field named "İÇERİK"
I want to enter data in this field using an Excel macro. But I can't enter data using the code:
Sub deneme()
Dim URL As String
On Error Resume Next
URL = "http://splan.byethost7.com/mesaj_yaz.php?fno=1&kip=yeni"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 1
For i = 1 To Range("A" & Rows.Count).End(3).row
If Cells(i, 1) <> Empty Then
ie.navigate URL
Call bekle
ie.Document.getElementById("mesaj_icerik").Value = "TEST"
ie.document.getElementsByName("mesaj_baslik").Item(0).Value = Cells(i, 1)
'IE.Document.getElementsByClassName("submitButton")(0).Click
Call bekle
End If
Next i
' IE.Quit
Set ie = Nothing
End Sub
Sub bekle()
With ie
Do Until .readyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
End With
Application.Wait (Now + TimeValue("00:00:02"))
End Sub
As I said in my comments, there are several issues with your code, although the overall effort is good.
Firstly, this ie.document.getElementsid("mesaj_baslik") is not a valid method. If what you want is to access a single HTML element with a unique ID, then the method you need to use is ie.Document.getElementById("the element's ID").
Assuming that what I wrote above is what you were trying to achieve, you have to keep in mind that the .getElementById() method, returns only one single element.
So this ie.Document.getElementById("the element's ID").item(0) would give you an error saying:
Object doesn't support this property or method.
Even if all the aforementioned mistakes were corrected, I still don't see any elements with an ID equal to "mesaj_baslik", in the HTML snippet that you have provided. In fact this particular string is nowhere to be found in the HTML.
So even if the method was correct, this ie.Document.getElementById("mesaj_baslik"), would return Nothing.
Secondly, although your usage of the method ie.document.getElementsByName() is correct, there is no element with a Name attribute being equal to "formlar_mesajyaz", in the HTML snippet you have provided.
In fact this string seems to be a Class name rather than anything else. In this case you would have to use this method: ie.document.getElementsByClassName().
Now, from the info you have provided, the best I can do is assume that, what you want to do is enter some text in the textArea element. To do that, you can use the element's ID like so:
ie.Document.getElementById("mesaj_icerik").Value = "TEST"

How to get actual Google search URL in Excel + How to extract website adress out of cell

I am stuck at the current situation. I have a couple of hundreds business names of which i have to search for the website adress.
Question 1:
What i have at the moment is an google search link in excel with the =hyperlink(etc,etc), with the business name and adress.
For example: http://www.google.com/search?hl=en&q=Airbus+12th street 12 +New York+&btnG=Google+Search"
But what i actually need is the url in the internet browser, which i get when i click on the excel hyperlink:
If i click on above link, this is the actual link i need: https://www.google.nl/search?hl=en&q=Airbus+12th+street+%C2%A012%C2%A0+New+York+&btnG=Google+Search&gws_rd=cr&dcr=0&ei=o3nLWqbjN87PwAKY54_oCg
My question, is it possible to do this automaticly and get the actual link in excel ?
Question 2:
In excel i have a whole cell full of data. In this data, there is 1 url. For example, cell contains: ''ujeri sfifs93 sfisdsu fiweu2 732732 fjsdfue http:www.iwantthislink.comm sdfsdf shsdf sju32u3un''
is is possible to automaticly extract a word from this cell, beginning with http://www. ? And do this for every cell i want ?
Thanks in advance!
Example for your first question. You could do this by navigating to the url you've got in your workbook, wait for the redirect then return the redirected url
Public Sub GetRedirectedLink()
Dim IE As Object
Dim InitialUrl As String, RedirectUrl As String
Set IE = CreateObject("InternetExplorer.Application")
InitialUrl = "http://www.google.com/search?hl=en&q=Airbus+12th%20street%2012+New%20York+&btnG=Google+Search"
With IE
.Visible = False
' Navigate to url
.navigate InitialUrl
' Wait while page loads
Do While .Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' return Redirected URL
RedirectUrl = .LocationURL
End With
MsgBox RedirectUrl
End Sub
For your second question you could use regular expressions to return the domain name (if that is the part that you want)

VBSCRIPT in UFT in Excel

Scenario: In an Excel Sheet there are URLs of 100 webpages.
UFT should iterate through each row, read the cell data(URL) from the Excel Sheet
Navigate to each URL and
Print the current webaddress of each of the URL after navigation.
Using: Browser("bname").Page("pname").GetROProperty("URL") would require to know the "pname" of each of the navigated URLs. This requires manual intervention and is hectic. Is there anyother way to print the current URL of each of the navigated webpage?
You can try something like this -
for your 1 step where you are reading the data from excel, you can use dictionary object or something to store each URL
then you can use something like -
a = "www.google.com" 'here you are need to parameterize each URL read from dictionary'
For i = 1 to 100 'this can made dynamic till the count of rows'
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate a
wait(2) 'you can use wait/sync dynamically'
newURL = Browser("creationtime:=0").page("title:=.*").GetROProperty("URL")
Print newURL 'here you can change it to log it to textfile or excel'
objIE.quit()
Next
this is just to brief an idea, you may chose to update/change based on your need.

Use excel vba to Input value on an .asp page

I'm writing VBA code in Excel to generate various reports.
Once it's done I would really like (and I would be looked at like a hero by my fellow co-worker) to input my results directly on our corporate intranet.
So I've started educating myself on how to use VBA to interact with Internet Explorer. I know get the basics so I can do cool stuff (but unusefull in this case ) like loading a web site. But when I try to input values in a text box on this page on the Intranet, I can't go any where.
I'm suspecting that the problem is caused by the fact that the adress I'm accessing is ending with .asp extension.
Here's the code I'm using below
Beware that I will most probably have other questions following this first one. You might just become my new-web-geek-bestfrient ;-)
Sub interaction()
'Variables declaration
Dim IE As New InternetExplorer
Dim IEDoc As HTMLDocument
Dim ZoneMotsClés As HTMLInputElement
'page to be loaded, it's on a corporate intranet
IE.navigate "http://intranet.cima.ca/fr/application/paq/projets/index.asp"
IE.Visible = True
Do ' Wait till the Browser is loaded
Loop Until IE.readyState = READYSTATE_COMPLETE
Set IEDoc = IE.document
'this is the text zone that I'm trying to input value into
Set ZoneMotsClés = IEDoc.getElementById("txtMotCle")
'this is where it crashes. At this point I'm only trying to enter a project number into
'the "txtMotCle" 'text zone
ZoneMotsClés.Value = "Q141763B"
'.....
Set IE = Nothing
Set IEDoc = Nothing
End Sub
So at this point (when I try to input the value in the text box I get a:
error 91 object variable or with block variable not set
and here's the html code of the section on the page I'm trying to write in.
<INPUT onfocus="javascript:document.frmMyForm.TypeRecherche.value='simple';"
style="FONT-SIZE: 9px; FONT-FAMILY: verdana" maxLength=250 size=60 name=txtMotCle>
This time I tried the suggestions of the 2 contributors (Tx Jeeped and Tim Williams) but still getting the same error 91.
Now I tried that modification (tx SeardAndResQ)
'this is the text zone that I'm trying to input into
'Set ZoneMotsClés = IEDoc.all("txtMotCle")
ID = "txtMotCle"
Set ZoneMotsClés = IEDoc.getElementById(ID)
'this is where it crashes. At this point I'm only trying to enter a project number into the "txtMotCle"
'text zone
ZoneMotsClés.Value = "Q141763B"
Same result. I'm not sure I made it the way #searchAndResQ meant it

Resources