how to select a specific dropdown element in webpage by excel vba - excel

Currently, I am working on a webpage to extract some data related to my work. Here I am facing a problem while selecting the drop-down menu in the production list. I want to select the "All" option in the drop-down list. By default, it takes only 25 values in the production table.
Here is my code
Sub Production_table()
Dim objIE As InternetExplorer
Dim ele As Object
Dim ele2 As Object
Dim ele3 As Object
Dim s As String
Dim i As Integer
i = 2
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While Range("A" & i).Value <> ""
Set objIE = New InternetExplorer
objIE.Visible = True
source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search
objIE.navigate source"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Sheets.Add
ActiveSheet.Name = "Data"
On Error Resume Next
Set ele2 = objIE.document.getElementById("productionTable_length"). _
getElementsByTagName("label")
For Each ele3 In ele2
If ele3.Value = -1 Then
ele3.Focus
ele3.Selected = True
ele3.Click
Exit For
End If
Next
webpage link-- https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search
Please help me on the same.

I always recommend using Chrome Browser for such cases as it let's you inspect the element in a userfriendly way.
Try this
Sub Sample()
Dim objIE As Object, ele As Object, opt As Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
Source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
objIE.navigate Source
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Set ele = objIE.document.getElementsByTagName("select")
For Each opt In ele
If opt.getAttribute("name") = "tblWellRecords_length" Then
opt.Focus
opt.Value = -1
Exit For
End If
Next opt
End Sub
I was looking for the "All" element to click on the production data. it's changing the value of the drop-down if I fix the attribute name as "productionTable_length" but it's not clicking the element. If it is clicked then the total number of shows will be changed from 1-25 to 1-166. Please help me on that.... – Partha 2 hours ago
You did not say about that :) Your question was how to select a specific dropdown element in webpage by excel vba For which I gave you the answer. For what you want, there is a different approach.
Try this
Sub Sample()
Dim objIE As Object, source As String, script As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
source = "https://secure.conservation.ca.gov/WellSearch/Details?api=01120716&District=&County=&Field=&Operator=&Lease=&APINum=&address=&ActiveWell=false&ActiveOp=false&Location=&sec=&twn=&rge=&bm=&PgStart=0&PgLength=10&SortCol=6&SortDir=asc&Command=Search"
objIE.navigate source
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
script = "$('select[name=productionTable_length]').val('-1'); $('select[name=productionTable_length]').trigger('change');"
objIE.document.parentWindow.execScript script, "jscript"
End Sub

Related

Retrieve the URL from the Internet Explorer

i am struggling on the following: I try to open an URL(Link), become redirected to a new URL and retrieve this new URL into a Cell in my excel worksheet.
I have written the following code but it is not retrieving the new URL and is not quitting the Internet Explorer at the end:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
ISIN = Range("A1").Value
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate Link
End With
Range("A2") = IE.LocationURL
Set IE = Nothing
IE.Quit
End Sub
The ISIN in A1 is KYG875721634.
I would be very glad if some of you guys could find the problem. Thank you very much!
Greetings, Robin
This works. Read the comments please:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
'ISIN = Range("A1").Value
ISIN = "KYG875721634"
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate Link
End With
'You must wait to load the page
Do While IE.readystate <> 4: DoEvents: Loop
'Range("A2") = IE.LocationURL
MsgBox IE.LocationURL
'First quit the IE
'because this step needs the reference ;-)
IE.Quit
Set IE = Nothing
End Sub
There're two issues in your code:
You need to add a wait to load page.
Do Until IE.readyState = 4
DoEvents
Loop
First quit IE. If you first set IE to Nothing then there will be no reference to IE when quit.
IE.Quit
Set IE = Nothing
The final code is like this which can work well:
Sub Get_URL()
Dim ISIN As String
Dim Link As String
Dim IE As Object
ISIN = Range("A1").Value
Link = "https://www.finanzen.net/suchergebnis.asp?_search=" & ISIN
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate Link
Do Until IE.readyState = 4
DoEvents
Loop
Range("A2") = IE.LocationURL
IE.Quit
Set IE = Nothing
End Sub
Result:

Retrieving a URL from Internet Explorer with VBA

I have written some VBA code in Excel to retrieve the latitude and longitude from a Google Maps URL and paste it into a cell in my worksheet. My problem is in retrieving the URL from internet explorer. Below I have two examples of my code, one macro returns an about:blank as though the object doesn't have the LocationURL property, and the other example seems like it is saving all of my previous searches, so it cycles through all of my previous searches and pastes the very first searches' URL. Example 2 uses a shell suggestion that I found online to reassign the properties to the oIE object. I can get both to slightly work, but neither will do exactly what I need from the macro.
Cell(8,8) is a hyperlink to google maps where I'm searching an address, and Cell(8,9) is where I want to paste the URL after google maps has redirected and has the latitude and longitude in the URL.
Example 1:
Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://www.google.com/maps?q=" & Range("I7").Value
Do
DoEvents
Loop Until ie.ReadyState = 4
Set Doc = ie.Document
Cells(8, 9).Value = ie.LocationName
End Sub
Example 2:
Sub Macro()
Dim oIE, oShell, objShellWindows, strPath, X
strPath = Cells(8, 8)
Set oIE = CreateObject("InternetExplorer.Application")
'This is to resolve oIE.navigate "about:blank" issue
oIE.Top = 0
oIE.Left = 0
oIE.Width = 500
oIE.Height = 500
oIE.Navigate strPath
Do While oIE.Busy And oIE.ReadyState < 2
DoEvents
Loop
'Reassigning oIE.LocationName & vbCrLf & oIE.LocationURL values after redirect in IE
Set oShell = CreateObject("WScript.Shell")
Set objShellWindows = CreateObject("Shell.Application").Windows
For X = objShellWindows.Count - 1 To 0 Step -1
Set oIE = objShellWindows.Item(X)
If Not oIE Is Nothing Then
If StrComp(oIE.LocationURL, strPath, 1) = 0 Then
Do While oIE.Busy And oIE.ReadyState < 2
DoEvents
Loop
oIE.Visible = 2
Exit For
End If
End If
Cells(8, 9).Value = oIE.LocationURL
Set oIE = Nothing
Next
Set objShellWindows = Nothing
Set oIE = Nothing
End Sub
Thanks,
Andrew
Is this as simple as looping until the document.URL changes? In my timed loop I wait for the string safe=vss in the original page load to disappear.
Option Explicit
Public Sub GetNewURL()
Dim IE As New InternetExplorer, newURL As String, t As Date
Const MAX_WAIT_SEC As Long = 5
With IE
.Visible = True
.navigate2 "http://www.google.com/maps?q=" & "glasgow" '<==Range("I7").Value
While .Busy Or .readyState < 4: DoEvents: Wend
t = Timer
Do
DoEvents
newURL = .document.URL
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While InStr(newURL, "safe=vss") > 0
Debug.Print newURL
End With
End Sub

VBA to select list in IE

I've been trying to use VBA to open an IE window and select from one of the dropdown list showing bwin but failed. Any advice?
Private Sub OKButton_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate "http://www.yahoo.com/"
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
Set AvailableLinks = oIE.document.getelementbyid("list-listing").getelementsbytagname("a")
For Each cLink In AvailableLinks
If cLink.innerhtml = "????" Then
cLink.Click
End If
Next cLink
End Sub

Cycling Through List of URLs

This code works great when cycling through URLs such as google, yahoo, ect
But I am really trying to cycle through web pages as shown here.
\\FMC9050101\Proj\6513_OAK3\Jobads\slide1.htm
\\FMC9050101\Proj\6513_OAK3\Jobads\slide2.htm
\\FMC9050101\Proj\6513_OAK3\Jobads\slide3.htm
The 1st webpage opens up perfect, but I get and Automation Error, "The object invoked has disconnected from its clients" on this line, as the next page is cycled in... the idea is to replace the existing page without opening a new tab.
While .Busy Or .ReadyState <> 4: DoEvents: Wend
**** Code ***
Dim wsSheet As Worksheet, Rows As Long, links As Variant, IE As Object, link As Variant
Set wb = ThisWorkbook
Set wsSheet = wb.Sheets("Sheet1")
Set IE = New InternetExplorer
Rows = wsSheet.Cells(wsSheet.Rows.Count, "A").End(xlUp).Row
links = wsSheet.Range("A1:A" & Rows)
With IE
.Visible = True
For Each link In links
.navigate (link)
While .Busy Or .ReadyState <> 4: DoEvents: Wend
MsgBox .Document.body.innerText
Next link
End With
Okay, changed the strategy where I am reading the list of URLs from a server verses an excel sheet which is where I was going with this after the other issues were sorted out. Using an Admin account, this version works perfectly.
Sub Run_SlideShow()
'
Dim x As Integer
Dim wsSheet As Worksheet, Rows As Long, links As Variant, IE As Object, link
As Variant
Dim FilePath As String, Filter As String, F As Variant, I As Integer
'
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = True
'
Set wb = ThisWorkbook
Set wsSheet = wb.Sheets("Sheet2")
Filter = "*.htm"
Set IE = CreateObject("Internetexplorer.Application")
IE.Visible = False
FilePath = "\\FMC9050101\PROJ\6513_OAK3\Jobads"
For x = 1 To 9999 ' run for 30 hours, use scheduled task to kill excel and
restart every 24 hours
'
ArrFile = GetFileList(FilePath + "\" + Filter)
Select Case IsArray(ArrFile)
Case True
For I = LBound(ArrFile) To UBound(ArrFile)
F = ArrFile(I)
link = (FilePath & "\" & F)
IE.Navigate link
IE.Visible = True
'Application.StatusBar = "Loading " & link
Do While IE.Busy
Application.Wait DateAdd("s", 2, Now) ' set slide time here
Loop
Next
Case False 'no files found
MsgBox "No matching files"
End Select
Next x
'
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
'
Set IE = Nothing
Application.StatusBar = ""
'
End Sub

Where to find VBA functions for working with internet explorer

I am working on a VBA function to go to a webpage, find a single HTML element, and display its contents. Here is what I have so far.
Function WebTableToSheet(webpage As String, tag As String, count As Integer) As String
'Tested using IE7, Excel 2000 SP1, and Windows XP
Dim objIE As Object
Dim varTables, varTable, document
Dim allTags, tags
Dim varRows, varRow
Dim varCells, varCell
Dim lngRow As Long, lngColumn As Long
Dim strBuffer As String
Set objIE = CreateObject("InternetExplorer.Application")
'Setup
With objIE
.AddressBar = False
.StatusBar = False
.MenuBar = False
.Toolbar = 0
.Visible = True
.Navigate webpage
End With
'Load webpage
While objIE.Busy
Wend
While objIE.document.ReadyState <> "complete"
Wend
varTable = objIE.document.All
allTags = objIE.document.All.tags(tag)
WebTableToSheet = allTags.Item(count)
Debug.Print "Testing function"
Cleanup:
Set varCell = Nothing: Set varCells = Nothing
Set varRow = Nothing: Set varRows = Nothing
Set varTable = Nothing: Set varTables = Nothing
objIE.Quit
Set objIE = Nothing
End Function
I am able to open InternetExplorer, got to the webpage specified in the function, but when I try searching for a specific tag, it seems to fail. I have had trouble searching for Microsoft VBA Internet Explorer documentation and knew what variables and methods are available for this task?
I'd recommend setting a reference to shdocvw.dll and the Microsoft HTML Object Library. In XP shdocvw.dll is in your system32 folder, the HTML Object Library should be in your list of references. I only have access to XP at the moment, so you'll have to do a search for any other ver of windows you're using. Then in your code you can do the following:
Dim IE as SHDocVw.InternetExplorer
Dim doc as HTMLDocument
Dim el as IHTMLElement
Set IE = new SHDocVw.InternetExplorer
With IE
.navigate "some.webpage.com"
.visible = true
End With
Do
'Nothing
Loop Until IE.readyState = READYSTATE_COMPLETE ' = 4
Set doc = IE.Document
Set el = doc.getElementById("someElementId")
If Not el Is Nothing Then
MsgBox el.innerText
End If
Team,
IE webpage full loading check use below mentioned code
Application.Wait (Now + TimeValue("0:00:1"))
Do Until IE.Busy = False
DoEvents
Loop
ieApp.Navigate "Https://duckduckgo.com/"
'Wait for Internet Explorer to finish loading
Do While ieApp.Busy: DoEvents: Loop
Do While ieApp.Busy And Not ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
ieApp.Document.getelementbyid("search_form_input_homepage").Value = "Gajendra Santosh"
ieApp.Document.getelementbyid("search_button_homepage").Click

Resources