lochnagar2020
New Member
- Joined
- Jun 19, 2020
- Messages
- 5
- Office Version
- 2010
- Platform
- Windows
Hello,
Many thanks for taking the trouble to click on this thread. This is my first time using Selenium / VBA to interact with a webpage, so apologies in advance.
I have a large number of fuel gas compositions which I wish to calculate the methane number and propane knock index of using the Wartsila online calculator, found here:
After some trial and error, it seems that I needed to Copy Full XPath and use FindElementByXPath, in order for VBA to paste the relevant MOL% figures into the correct boxes, click the CALCULATE button and copy the results into Excel.
However, as you'll see below, the XPaths are quite long - I had hoped to use the FindElementByName function, and use the component names e.g. methane, ethane etc., but the boxes don't seem to have html Name(s) or ID(s).
1) methane box XPath: /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input
2) ethane box XPath: /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input
I was hoping to use a loop for populating the MOL% boxes by incrementing the tr index, but the FindElementByXPath expects the string in parentheses e.g.
.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input")
I had tried the following:
.FindElementByXPath(xPathStart & 1 & xPathEnd)
where:
xPathString = "/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr["
xPathEnd = "]/td[2]/input"
...but no joy!
I appreciate it's easy enough to paste /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input into a worksheet, generate the correct XPaths (using excel functions) for all components and paste them into VBA; but a loop is more elegant. I just lack the skill / knowledge, and was hoping someone could help.
I've pasted a copy of my test subroutine below, it only changes the methane and ethane values, hence the test.
Any help etc. would be greatly appreciated.
Kind regards,
Lochnagar2020
Private mybrowser As Selenium.ChromeDriver
Sub Macro1()
Set mybrowser = New Selenium.ChromeDriver
mybrowser.Start baseUrl:="Wärtsilä methane number calculator"
mybrowser.Get "/"
Set methaneNumber = mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[2]/td[2]/input")
Set PKI = mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[3]/td[2]/input")
'// click Tracking Consent Dialog box
mybrowser.FindElementById("tracking-consent-dialog-accept").Click
'// clear pre-populate methane mole percentage then add mol% = 70
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input").Clear
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input").SendKeys "70"
'// enter ethane mole percentage then add mol% = 30
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input").Clear
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input").SendKeys "30"
'// click calculate button
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[1]/td/button").Click
'// wait for calculator to calculate methane number and propane knock index
While methaneNumber.Value = 0
Wend
'// print methane number and propane knock index to Immediate window
Debug.Print methaneNumber.Value
Debug.Print PKI.Value
End Sub
Many thanks for taking the trouble to click on this thread. This is my first time using Selenium / VBA to interact with a webpage, so apologies in advance.
I have a large number of fuel gas compositions which I wish to calculate the methane number and propane knock index of using the Wartsila online calculator, found here:
Wärtsilä methane number calculator
This calculation tool provides guidance about the Methane Number and the result gives an indication if gas is suitable for Wärtsilä Dual Fuel engines.
www.wartsila.com
After some trial and error, it seems that I needed to Copy Full XPath and use FindElementByXPath, in order for VBA to paste the relevant MOL% figures into the correct boxes, click the CALCULATE button and copy the results into Excel.
However, as you'll see below, the XPaths are quite long - I had hoped to use the FindElementByName function, and use the component names e.g. methane, ethane etc., but the boxes don't seem to have html Name(s) or ID(s).
1) methane box XPath: /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input
2) ethane box XPath: /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input
I was hoping to use a loop for populating the MOL% boxes by incrementing the tr index, but the FindElementByXPath expects the string in parentheses e.g.
.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input")
I had tried the following:
.FindElementByXPath(xPathStart & 1 & xPathEnd)
where:
xPathString = "/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr["
xPathEnd = "]/td[2]/input"
...but no joy!
I appreciate it's easy enough to paste /html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input into a worksheet, generate the correct XPaths (using excel functions) for all components and paste them into VBA; but a loop is more elegant. I just lack the skill / knowledge, and was hoping someone could help.
I've pasted a copy of my test subroutine below, it only changes the methane and ethane values, hence the test.
Any help etc. would be greatly appreciated.
Kind regards,
Lochnagar2020
Private mybrowser As Selenium.ChromeDriver
Sub Macro1()
Set mybrowser = New Selenium.ChromeDriver
mybrowser.Start baseUrl:="Wärtsilä methane number calculator"
mybrowser.Get "/"
Set methaneNumber = mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[2]/td[2]/input")
Set PKI = mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[3]/td[2]/input")
'// click Tracking Consent Dialog box
mybrowser.FindElementById("tracking-consent-dialog-accept").Click
'// clear pre-populate methane mole percentage then add mol% = 70
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input").Clear
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[1]/td[2]/input").SendKeys "70"
'// enter ethane mole percentage then add mol% = 30
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input").Clear
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[1]/tbody/tr[2]/td[2]/input").SendKeys "30"
'// click calculate button
mybrowser.FindElementByXPath("/html/body/main/div[2]/div[2]/div/div[1]/table[3]/tbody/tr[1]/td/button").Click
'// wait for calculator to calculate methane number and propane knock index
While methaneNumber.Value = 0
Wend
'// print methane number and propane knock index to Immediate window
Debug.Print methaneNumber.Value
Debug.Print PKI.Value
End Sub