Hi everyone,
I'm completely new to VBA and since IE has ended it's lifecycle support our company will remove it from our OS soon for security reasons. I've had a working macro to copy data from an excel file to our website that contained some sort of form field structure.
I'm trying to convert the below set of instructions to work with selenium. But I've not been able to do it since I do not know how to work with WebDriver, WebElements nor VBA.
Few notes:
To be able to use the FindElementById I've had to use the switchtoframe -> cd.SwitchToFrame ("ptifrmtgtframe") because otherwise it wouldn't find the IDs.
I will need to clear the input box before entering data, otherwise it will copy it next to the text that was already on the box, something like this ->
'Clear the input box to enter new text
cd.FindElementById("BUSINESS_UNIT$" & i - 2).Clear
The steps of my code will need to change slightly from the previous code because the IE code didn't need to open a new browser instance.
- Open new instance of Chrome (even thought that I would prefer if we navigate to the exact site on the website and the code can run from there, but seems that I need to run a new chrome instance with selenium each time I want to use this macro).
- Because we need to open a new instance, we need first to login to the site each time the macro is used. So before proceeding, I'm checking if the tab name is "Create/Update Journal Entries". If it matches, that will mean that the user logged in.
If better way doing the check than using a GoTo... in case the tab name doesn't match. Please change it I didn't know how to do the loop and the GoTo worked (I know it is not good practice, but I'm not an expert sorry).
- Switch to proper iFrame to be able to find find elements by ID, otherwise it doesn't find anything.
- I guess some variables will need to be setup.
- Then the For Each loop
This is how the site looks like:
We have to add as many lines as our worksheet has rows with data. So with more rows will look like this:
That is why the loop, to cycle to each row on the excel sheet and copy the data to this form with input boxes. And that is why the ID will need to have a variable "BUSINESS_UNIT$" & i - 2 because it increases for each line.
Line 1 will be BUSINESS_UNIT$0
Line 2 will be BUSINESS_UNIT$1
The same applies for the other web elements by ID
Also as you can see there are by default some fields that have text already (Unit and Currency), but in the excel sheet the data may change, so the input box needs to be cleared before hand and then copy whatever is on the excel sheet.
This is the code I've prepared, but definitely isn't even close to achieving anything... that's where I need some help. I'm sharing it to see if it helps a little bit.
As you may see in the code that I've been writing.. I'm quite lost. I've been finding pieces, but I do not know how to end the puzzle and how some pieces go with others.
If you can help me, I will appreciate it eternally.
Thank you very much.
I'm completely new to VBA and since IE has ended it's lifecycle support our company will remove it from our OS soon for security reasons. I've had a working macro to copy data from an excel file to our website that contained some sort of form field structure.
I'm trying to convert the below set of instructions to work with selenium. But I've not been able to do it since I do not know how to work with WebDriver, WebElements nor VBA.
Few notes:
To be able to use the FindElementById I've had to use the switchtoframe -> cd.SwitchToFrame ("ptifrmtgtframe") because otherwise it wouldn't find the IDs.
I will need to clear the input box before entering data, otherwise it will copy it next to the text that was already on the box, something like this ->
'Clear the input box to enter new text
cd.FindElementById("BUSINESS_UNIT$" & i - 2).Clear
The steps of my code will need to change slightly from the previous code because the IE code didn't need to open a new browser instance.
- Open new instance of Chrome (even thought that I would prefer if we navigate to the exact site on the website and the code can run from there, but seems that I need to run a new chrome instance with selenium each time I want to use this macro).
- Because we need to open a new instance, we need first to login to the site each time the macro is used. So before proceeding, I'm checking if the tab name is "Create/Update Journal Entries". If it matches, that will mean that the user logged in.
If better way doing the check than using a GoTo... in case the tab name doesn't match. Please change it I didn't know how to do the loop and the GoTo worked (I know it is not good practice, but I'm not an expert sorry).
- Switch to proper iFrame to be able to find find elements by ID, otherwise it doesn't find anything.
- I guess some variables will need to be setup.
- Then the For Each loop
This is how the site looks like:
We have to add as many lines as our worksheet has rows with data. So with more rows will look like this:
That is why the loop, to cycle to each row on the excel sheet and copy the data to this form with input boxes. And that is why the ID will need to have a variable "BUSINESS_UNIT$" & i - 2 because it increases for each line.
Line 1 will be BUSINESS_UNIT$0
Line 2 will be BUSINESS_UNIT$1
The same applies for the other web elements by ID
Also as you can see there are by default some fields that have text already (Unit and Currency), but in the excel sheet the data may change, so the input box needs to be cleared before hand and then copy whatever is on the excel sheet.
VBA Code:
Sub JournalIE()
Dim IE As InternetExplorer
Dim ieDoc As HTMLDocument
Dim tbxNameFld As HTMLInputElement
Dim winShell As New ShellWindows
For Each IE In winShell
If IE.LocationURL Like "*https://website/*" Then
Set ieDoc = IE.document
Set frm = ieDoc.frames(0).document.forms
For i = 2 To Range("G65536").End(xlUp).Row
frm(0).all.Item("BUSINESS_UNIT$" & i - 2).Value = Cells(i, 7).Value
If frm(0).all.Item("ACCOUNT$" & i - 2).Value = "" Then frm(0).all.Item("ACCOUNT$" & i - 2).Value = Cells(i, 8).Value
If frm(0).all.Item("DEPTID$" & i - 2).Value = "" Then frm(0).all.Item("DEPTID$" & i - 2).Value = Cells(i, 9).Value
If frm(0).all.Item("PRODUCT$" & i - 2).Value = "" And Cells(i, 10).Value <> "" Then frm(0).all.Item("PRODUCT$" & i - 2).Value = Cells(i, 10).Value
If frm(0).all.Item("PROJECT_ID$" & i - 2).Value = "" And Cells(i, 11).Value <> "" Then frm(0).all.Item("PROJECT_ID$" & i - 2).Value = Cells(i, 11).Value
If frm(0).all.Item("FOREIGN_CURRENCY$" & i - 2).Value = "" Then frm(0).all.Item("FOREIGN_CURRENCY$" & i - 2).Value = Cells(i, 12).Value
If frm(0).all.Item("FOREIGN_AMOUNT$" & i - 2).Value = "" Then frm(0).all.Item("FOREIGN_AMOUNT$" & i - 2).Value = Replace(Cells(i, 13).Value, ".", ",")
If frm(0).all.Item("JRNL_LN_REF$" & i - 2).Value = "" Then frm(0).all.Item("JRNL_LN_REF$" & i - 2).Value = Replace(Cells(i, 14).Value, ".", ",")
frm(0).all.Item("LINE_DESCR$" & i - 2).Value = Cells(i, 15).Value
Next i
Exit For
End If
Next IE
End Sub
This is the code I've prepared, but definitely isn't even close to achieving anything... that's where I need some help. I'm sharing it to see if it helps a little bit.
VBA Code:
Sub JournalChrome()
Dim cd As New WebDriver
cd.Start "chrome", ""
cd.Get "https://website"
'Verify browser Title
'If Title doesn't match, come back here and check again.
SiteCheck:
Dim SearchTitle As Selenium.WebElement
If cd.Window.Title = "Create/Update Journal Entries" Then
'Switch to inner iFrame to be able to search inside for IDs
cd.SwitchToFrame ("ptifrmtgtframe")
'Set variables:
''''' Some variables will need to be setup to make the loop work? '''''
''''' HERE should go the For Each loop '''''
For Each cd In "I DO NOT KNOW WHAT GOES HERE"
For i = 2 To Range("G65536").End(xlUp).Row
''''' Example of finding element by ID-> cd.FindElementById("BUSINESS_UNIT$" & i - 2)
Next i
Exit For
End If
Next cd
MsgBox "You need to login"
GoTo SiteCheck
End Sub
As you may see in the code that I've been writing.. I'm quite lost. I've been finding pieces, but I do not know how to end the puzzle and how some pieces go with others.
If you can help me, I will appreciate it eternally.
Thank you very much.