debugging mode and run mode behave different on selenium VBA (Excel) - excel

I have searched for a while but cannot find a solution. also find similar link on this page and it is locked due to duplcation but actually it is not duplicate of anything. this is not Java or #c or anyother mentioned languages.
this is vba and selenium codes.
on below link on debugging mode (F8) everything working smoothly however on run mode (F5) it is not filling datefrom and dateto section or filling partily.
do you have any idea? also web site is a corporate web site so unfortunately I cannot share it.
Option Explicit
Sub Download_Report4()
Dim obj As New Selenium.WebDriver
Dim ele As WebElement
obj.Start "Chrome"
obj.Get "https://test.com"
Dim Username, Password As Range
Set Username = ActiveWorkbook.Sheets("LoginData").Range("B1")
Set Password = ActiveWorkbook.Sheets("LoginData").Range("B2")
obj.FindElementById("UserName").SendKeys Username.Text
obj.FindElementById("Password").SendKeys Password.Text
obj.FindElementById(LoginButton").Click
Application.Wait Now + TimeValue("00:00:02")
Dim Rows, x
For x = 2 To Sheet1.UsedRange.Rows.count Step 1
If IsEmpty(Sheet1.Range("A" & x)) Then
Exit For
Else
obj.FindElementByName("date$From").Clear
obj.FindElementByName("date$To").Clear
obj.FindElementByName("date$From").SendKeys ("01,03,2017")
obj.FindElementByName("date$To").Click
obj.FindElementByName("date$To").SendKeys (Sheet1.Range("B" & x))
obj.FindElementById("btnSearch").Click
obj.FindElementById("btndownload").Click
End If
Next
End Sub
by the way, date format is correct. when it is entered as 01/03/2017 chrome add dates odd. month section to date year is correct and nothing add to month section.
I will giving little bit more details.
on below code for internet explorer is working very well unless after download button click, ie 11 prompt "do you want to open or save" question stops my macro and loops ending. this is the reason why I move the macro selenium.
Public Sub Download_Report()
Dim ie As New InternetExplorer
Dim myElem As Variant
ie.Visible = True
ie.navigate "https://test.com"
ie.document.getElementById("UserName").Focus
ie.document.getElementById("LUserName").Value = Sheets("LoginData").Range("B1").Text
ie.document.getElementById("Password").Focus
ie.document.getElementById("Password").Value = Sheets("LoginData").Range("B2").Text
ie.document.getElementById("LoginButton").Click
End If
Dim Rows, x
For x = 2 To Sheet1.UsedRange.Rows.count Step 1
If IsEmpty(Sheet1.Range("A" & x)) Then
Exit For
Else
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementsByName("date$From")(0).Value = Sheet1.Range("A" & x).Text
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementsByName("date$To")(0).Value = Sheet1.Range("B" & x).Text
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementById("btnSearch").Click
While .Busy Or .readyState < 4: DoEvents: Wend
ie.document.getElementById("btndownload").Click
End If
Next
End With
End Sub
date can be use as picker after clicked but also can be filled as text also, date section code on web site as is below. also date to section is as same as .
Website code (website is behind a firewall)
<input name="date$From" type="text" value="18/02/2020" size="6" id="date_tbFrom" class="searchSelect">
<input type="hidden" name="date_From_Client" id="date_From_Client">

yeah there were issue that (I do not know the why but), when .click command is applied to date section cursoir was choosen the half of the section or the end so date was entered by macro is wrong or section cannot be filled that means empty. however at debugging mod everything was working smoothly. than I find another solution. I have find sections one tab before date sections. then added
obj.FindElementByName("date$From").Clear
obj.FindElementByName("date$To").Clear
obj.findelementsbyid("sectionbeforedatefrom").click
obj.sendkeys(keys.Tab)
obj.FindElementByName("date$From").SendKeys ("01,03,2017")
obj.findelementsbyid("sectionbeforedateto").click
obj.sendkeys(keys.Tab)
obj.FindElementByName("date$To").SendKeys (Sheet1.Range("B" & x))
obj.FindElementById("btnSearch").Click
those steps worked for me. but #TimWilliams was right all things are related with time. thanks everyone.

Related

Get text from web site

I'm trying to get the content of a web site and paste it onto a excel sheet.
Before trying to code something in VBA I was simply doing "Control + A" (to select ALL the content from the page I want), "Control + C" (to copy it to the clipboard) and then "Control + V" (to paste it in my sheet).
This sort of information is all I want, this is all I need.
The way I will manipulate the data is another story and it's all working just fine.
I need no other way to get information from the web site then the one that "Control + A" and "Control + C" provides me.
That being said, this is the code I've found and which I'm trying to make it work:
Sub Get_Text_From_Page()
Dim sURL As String
sURL = "https://www.microsoft.com"
Dim IE As Object
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate (sURL)
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Range("A1").Value = .Document.body.innerText
End With
End Sub
This opens a browser and then it crashes with an Automation Error at "While .Busy Or .ReadyState <> 4: DoEvents: Wend".
I've found a few code examples in many places, but they all crash with that Busy or ReadyState stuff...
Edit: Not sure if this is gonna take me anywhere, but I've just found something about SendKeys "^a" and then SendKeys "^c".
You could try to refer to the code example below.
It will launch the IE browser. Open the website. After the site gets loaded, it will select all the content, copy it, and paste it to sheet1 in Excel.
Sub demo()
Dim IE As Object
Sheets("Sheet1").Select
Range("A1").Select
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://microsoft.com"
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
IE.ExecWB 17, 0
IE.ExecWB 12, 2
Range("A1").Select
ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
IE.Quit
End Sub
You could try it and let us know whether it works for you.
If you have some questions about the code, please let me know about it.

VBA to click on a button on an IE form to submit it

I'm quite new to VBA so please bear with me. I've been trying to create an automation to fill in username and password and login to a site (to start with) but I've been having trouble trying to click on the submit button. Scoured the internet and learnt a whole bunch of things but I didnt find anything that seems to work. The page loads and fills in the details and nothing happens when I run the code below.
Would greatly appreciate some help with this. Thanks in advance as always!
Sub worldcheck()
Dim lastrow As Long
Dim IE As Object
Dim cel As Range
Dim post As Object
Dim ws As Worksheet
Dim element As Object
Set ws = Sheets("sheet1")
Set IE = CreateObject("internetexplorer.application")
lastrow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
IE.Visible = True
IE.Navigate "https://www.world-check.com/frontend/login/"
Do While IE.busy
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:2"))
IE.document.getElementbyID("username").Value = ws.Range("D2")
IE.document.getElementbyID("password").Value = ws.Range("D3")
IE.document.getElementbyClass("button").click
Do While IE.busy
DoEvents
Loop
End Sub
Nothing else happens? You should be getting an error message at the very least as you are trying to use a non existent method (VBA Run-time error 438 Object doesn't support this property or method) . The method is getElementsByClassName - note the s indicating it returns a collection and the ending is ClassName. You would then need to index into that collection before attempting to access the Click method
As there is only a single element with classname button you can use a faster css class selector (this is also faster than using a type selector of form; likewise, you can use the faster css equivalent of getElementById for the other two DOM elements). document.querySelector stops at the first match so is also more efficient.
Finally, rather than hard coded waits use proper page load waits as shown below:
Option Explicit
Public Sub WorldCheck()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://www.world-check.com/frontend/login/"
While .busy Or .readystate <> 4: DoEvents: Wend
With .document
.querySelector("#username").Value = "ABCDEF" ' equivalent to .getElementbyID("username").Value = "ABCDEF"
.querySelector("#password").Value = "GHIJKL" '.getElementbyID("password").Value = "GHIJKL"
.querySelector(".button").Click
End With
While .busy Or .readystate <> 4: DoEvents: Wend
Stop '<== delete me later
.Quit
End With
End Sub

Assign a value from excel to a html input box through excel vba

I have some ISIN-codes from bonds in an excel sheet (column A). I would like to find the corresponding rating from the site https://www.moodys.com.
As this should be done on a regular basis, I would like to automate this process through Excel VBA.
The website does not use any ID's so I can't use getElementById.
I tried using through getElementsByClassName, but this does not work. Also I am not sure if the "search-widget" is the correct class name.
The Error message is:
Object doesn't support this property or method (Error 438)
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Nbr = ThisWorkbook.Sheets("Sheet1").Cells(1, 1).End(xlDown).Row - 1
With Worksheets("Sheet1").Range(Cells(2, 2), Cells(100000, 6))
.ClearContents
End With
For i = 1 To 1 'Nbr
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.moodys.com"
'wait here a few seconds while the browser is busy
Application.Wait (Now + TimeValue("00:00:04"))
'Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do
DoEvents
Loop Until objIE.readyState = 4
objIE.document.getElementsByClassName("search-widget").Value = Sheets("Sheet1").Range("A" & i + 1).Value
...
The getElementsByClassName method returns an array of all elements with that classname, even if that is only one element. So if you know it is the first and only element with that classname, you can use getElementsByClassName("search-widget")(0).Value
Furthermore, you can find the right classname by inspecting the pages html. In chrome, you can do that by right clicking the element and press "inspect". The only searchbar I saw on the site has "typeahead search-box-input left" as classname by the way.

VBA: Search for text in IE, open link, find line copy text, insert in Excel

I'm a complete newbie. Can you guys help me with the following?
1. I have a spreadsheet with productcodes.
2. I have an active Internet Explorer session (logged in) on an index page filled with links that among others contain the said productcodes.
3. For each code, I need it to find the code, open the link. Then find a line containing the string "udsalg" and copy the complete line i.e. "Udsalg 10994"
4. Insert the string next to the productcode.
5. Enter IE and "go back" to index page.
5. Rinse and repeat until all productcodes have been searched.
I have no skill regarding this and hoping for help. I understand basic programming (php etc). I hope somebody can help or point in the right direction. Best regards.
You can try to refer code example below will give you most of the answers for your above questions. Note that this is not the exact code.
'start a new subroutine called SearchBot
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result as String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://duckduckgo.com"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("search_form_input_homepage").Value = _
Sheets("Sheet1").Range("A2").Value & " in " & Sheets("Sheet1").Range("C1").Value
'click the 'go' button
objIE.document.getElementById("search_button_homepage").Click
'wait again for the browser
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'the first search result will go in row 2
y = 2
'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.document.getElementsByClassName("result__a")
'...get the href link and print it to the sheet in col C, row y
result = aEle
Sheets("Sheet1").Range("C" & y).Value = result
'...get the text within the element and print it to the sheet in col D
Sheets("Sheet1").Range("D" & y).Value = aEle.innerText
Debug.Print aEle.innerText
'is it a yellowpages link?
If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
'make the result red
Sheets("Sheet1").Range("C" & y).Interior.ColorIndex = 3
'place a 1 to the left
Sheets("Sheet1").Range("B" & y).Value = 1
End If
'increment our row counter, so the next result goes below
y = y + 1
'repeat times the # of ele's we have in the collection
Next
'add up the yellowpages listings
Sheets("Sheet1").Range("B1").Value = _
Application.WorksheetFunction.Sum(Sheets("Sheet1").Range("B2:B100"))
'close the browser
objIE.Quit
'exit our SearchBot subroutine
End Sub
You can try to refer the code and try to understand it. Then you can try to modify it based on your requirement. If you have any further questions than you can try to post your sample code. We will try to check it and try to provide you suggestions for it.
Helpful References to learn about IE VBA Automation.
(1) Excel + VBA + IE = web automation
(2) IE (Internet Explorer) Automation using Excel VBA
(3) Automate Internet Explorer (IE) Using VBA

Windows 10 Internet Explorer is not working with old VBA code

I wrote code in VBA a couple years ago to open a website in IE and fill in textboxes with data from an excel file. Unfortunately I am using windows 10 now and this program is not working anymore. It opens the website with no problem, but cannot transpose the data into the textboxes. It simply opens the website and stalls (no data is entered).
The program still works in IE in windows 7, without any problem. I tried multiple laptops with windows 10 and the problem reoccurs.
I honestly don't know how to fix this.
DoEvents
WebAddress = "CONFIDENTIAL URL HERE" & WebID & "&t="
Dim IE As Object
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate WebAddress
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
Application.Wait (Now + TimeSerial(0, 0, 4))
IE.Document.All("Response_123").Value = ThisWorkbook.Sheets("Sheet1").Range("B5")
The last line of code should transfer data from the excel file to the textbox in internet explorer, however nothing happens and the textbox remains blank. Do I need to change anything in my code to account for windows 10 IE?
You can see that your code was not written in an efficient way. For an alternative approach, You can refer example below which is working fine with Windows 10.
Dim IE As Object
Sub demo()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "C:\Users\Administrator\Desktop\sample.html"
While IE.Busy
DoEvents
Wend
wait 1
IE.Document.getElementById("firstname").Value = ThisWorkbook.Sheets("Sheet1").Range("A1")
wait 1
IE.Document.getElementById("lastname").Value = ThisWorkbook.Sheets("Sheet1").Range("A2")
wait 1
IE.Document.getElementById("submit_btn").Click
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Private Sub wait(seconds As Long)
Dim endTime As Date
endTime = DateAdd("s", seconds, Now())
Do While Now() < endTime
DoEvents
Loop
End Sub
Output:
If we talk about your above posted code than you can try to put the break point on that line and try to debug the code. Check that Which value contained by Cell B5 and check whether that line get execute successfully or not. For testing purpose, try to assign static value to that textbox.

Resources