Get XPATH for selenium VBA - excel

I am struggling with a web page on a secure website. I will put a snapshot of what I am working on.
The XPATH that has the rows of a table (equals to 13 rows) is that
//div[#id='Section3']
But the data is not inside that XPATH but after it in 9 columns.
How can I refer to those children or ancestors ( I don't know the exact term)?
Here's the HTML for that page ( I couldn't include it in the question)
https://pastebin.com/hEq8K75C
Here's the snapshot (may clarify the issue well)
How to implement the variable j in such lines?
Dim x As Long, i As Long, j As Long
x = .FindElementsByXPath("//div[#id='Section3']")
For i = 1 To x
For j = 1 To 9
Cells(i, j).Value = .FindElementByXPath("//div[#id='Section3']/following-sibling::div[following-sibling::div[#id='Section3'][count(preceding-sibling::div[#id='Section3'])=" & i & " and count(following-sibling::div[#id='Section3'])=" & x - (i + 1) & "]][" & j & "]").Text
Next j
Next i

Try to use below code:
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[#id='Section3']/following-sibling::div[count(preceding-sibling::div[#id='Section3'])={0} and count(following-sibling::div[#id='Section3'])={1}]"
for i in range(counter):
print('\nRow #{} \n'.format(i + 1))
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
for cell in cells:
value = cell.find_element_by_xpath(".//td").text
print(value)

Related

Listbox not displaying proper vlookup result

I am trying to execute this part of the code.
Do While ws2.Cells(i, "A").Value <> Empty
arr2(k, j) = ws2.Cells(i, "A").Value
arr2(k, j + 1) = Application.VLookup(ws2.Cells(i, "A").Value, ws.Range("B:K"), 2, False)
formula = ("VLOOKUP($A" & i & "&" & User.TextBox1.Value & ", CHOOSE({1,2}, 'DATA'!$B$4:$B$7669&'DATA'!$D$4:$D$7669,'DATA'!$E$4:$E$7669),2,0)")
q = CStr(Evaluate(formula))
arr2(k, j + 2) = q
k = k + 1
i = i + 1
Loop
I don't know why, but I get different results at times, without making any changes. Sometimes it shows error for the vlookup value but sometimes it shows correct number. What can I do to fix this problem?
You are not specifying on which sheet the evaluate should run.
So the $A" & i & .. is looking on the active sheet.
Change
q = CStr(Evaluate(formula))
to
q = CStr(ws2.Evaluate(formula))

Chart Naming with Variable in VBA, Excel 2013

I was trying to rename a chart with:
With ActiveChart
For i = 1 To Count_Rows
j = i + 1
.FullSeriesCollection(i).Name = """=Sheet1!$A$" & j & Chr(34)
Next i
...
this is resolving to desired formula as "=Sheet1!$A$2" and so on...
but not naming the series with contents instead putting the name of the series as "=Sheet1!$A$2"
You are not using a range object to return the cell values.
For i = 1 To Count_Rows
j = i + 1
.FullSeriesCollection(i).Name = Range("Sheet1!$A$" & j)
Next i

EXCEL VBA with VBS SAP session.findById variable not working

I need to loop this section and overwrite the x marked positions with my variable x value but if i use it as it is shown on the pic nothings happen. If i enter any value like 1 or 2 instead of x it works. How can i use a variable instead of a value in these places?
Here is also the code:
For j = 1 To SumPositionen
If Sheets("Datenverarbeitung").Cells(Z, 4) = tempCommodityForm Then
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1,x]").Text = Sheets("Datenverarbeitung").Cells(a, 5) 'Eingabe Materialnummer in 1. Position
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/txt/HERA/TRDINOUT_S-MFQUAN[2,x]").Text = Sheets("Datenverarbeitung").Cells(a, 6) 'Eingabe Menge in 1. Position
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MFU[3,x]").Text = Sheets("Datenverarbeitung").Cells(a, 7) 'Eingabe UoM
x = x + 1
End If
a = a + 1
Next j
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1,x]").Text
Here your string includes the literal value "x". What you really want is likely something more like:
session.findById("wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/TRDINOUT_S-MATNR[1," & x & "]").Text
So something like:
Dim s As String, sht As Worksheet
'....
'....
Set sht = Sheets("Datenverarbeitung")
s = "wnd[0]/usr/tabsTAB_DETAILS/tabpTAB_MF/ssubTABMF:/HERA/" & _
"TRDMF10:2023/tbl/HERA/TRDMF10TC_2023/ctxt/HERA/"
For j = 1 To SumPositionen
If sht.Cells(Z, 4) = tempCommodityForm Then
session.findById(s & "TRDINOUT_S-MATNR[1," & x & "]").Text = sht.Cells(a, 5) 'Eingabe Materialnummer in 1. Position
session.findById(s & "TRDINOUT_S-MFQUAN[2," & x & "]").Text = sht.Cells(a, 6) 'Eingabe Menge in 1. Position
session.findById(s & "TRDINOUT_S-MFU[3," & x & "]").Text = sht.Cells(a, 7) 'Eingabe UoM
x = x + 1
End If
a = a + 1
Next j
define data type to variable link string and integer

Vba how to compare 2 column

I'm making an heuristic analyse and i have the fallowing problem : I want to find in column D numbers that match with column J and replace them by a "0". You can see what I'm trying to do on this image :
Problem : Column D have multiples values per cell and column J have one value per cell.
some part of the code:
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
*For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
Range("D" & i).Value = vec(x)
End If
Next
Next
Next
*-> Here it is the problem, i cant figured out how to pass over the coma "," in column D,and store the data. I want to compare the temp with value on "d", but "d" can i have multiple numbers on the same cell, like " 3, 2, 1", and if there is any match like temp = 3, then d= "0,2,1".
English is not my native language so i hope you can understand what i want.
Thanks!
I think your almost there you just need to split up each cell and then search then recreate and replace the string in the cell. Please note I've not tested this.
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
Vec = split(d, ",") 'split the cell
d = "" 'clear the string
For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
End If
d = d & vec(x) & "," 'recreate the string
Next
Range("D" & i).Value = left(d, len(d) - 1) 'save the string without the last ,
Next
Next
you can do this with the below formula - no need for VBA
Make a new column somewhere near column D.
In your new column, use this FIND and Substitute formula:
=IF(NOT(ISERROR(FIND(J:J,D:D))),SUBSTITUTE(D:D,J:J,"0"),D:D)
This formula looks for the value in column J within the cells in column D.
If a match is found, 0 is substituted for the found number. Otherwise, column D is returned.
If you want, you can hide column D to avoid confusion.
in VBA you have a string function called Split(vString, delimiter) that will split a string into tokens using the specified delimiter. See
MSDN Library: VB Split Function
examples:
Mr Spreadsheet John Walkenbach: Split Function examples
VB-Helper

Multiple array assignment to an excel range

Just out of curiosity ,I am asking you a question which is as below:
Suppose i do have an array A1(6)=(45,25,,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20,25,48).Now can we put those array values with the help of a single statement like single array assignment to a row,as here all the rows to a range of an Excel, Say here "C1:R3" range.
EDIT
If I need to assign them to a row like R1<- A1 + A2 +A3,R2<- A1 + A2 +A3. Can you tell me how to this?
R1<- (45,25,,36,88),14,25,11,,,,11,21,20,25,48,) same for R2.
Thanks,
Dim A(2,5)
For i = 0 to 5
A(0, i) = A1(i)
A(1, i) = A2(i)
A(2, i) = A3(i)
Next i
Range("C1:R3").Value = A
EDIT
For second part, to the best of my understanding:
Dim R(17)
For i = 0 To 2
For j = 0 To 5
R(6 * i + j) = A(i, j)
Next j
Next i
Range("C5:T5").Value = R
EDIT 2
Alternatively:
Dim R
R = Split(Join(A1, ",") & "," & Join(A2, ",") & "," & Join(A3, ","), ",")
Range("C5:T5").Value = R
You can use any delimiter you like (if it's appropriate for your data).

Resources