1

I am hoping for some help with syntax in Selenium VBA.

I am trying to scrape some table date using a variable predicate 'r' to specify the row like this but it does not work.

The code does work when an integer value is put in in place of a predicate 'r' [I think predicate is the right word from looking around] so I know that the expression is finding the right thing.

For r = 2 To 4

ThisWorkbook.Sheets("Orders").Cells(r, 1).Value = bot.FindElementByXPath("//*[@id='ctl00_mainContent_ucOrdersList_dgOrders']/tbody/tr[r]/td[2]").Text

Next r

It seems as though it should be possible. I found this but am not sure what language it is or how to translate it into Selenium VBA:

for (int i =1;i<rows.size();i++)
            {    
                max= wd.findElement(By.xpath("html/body/div[1]/div[5]/table/tbody/tr[" + (i+1)+ "]/td[4]")).getText();

So in essence how do I do the tr[" + (i+1)+ "] bit in Selenium VBA?

Thanks in advance.

3
  • Use ampersand & to join strings in VB try "tr[" & (i+1) &"]" - i should write an answer for it :-) Commented Aug 5, 2020 at 13:31
  • Also - predicate pretty much means to evaluate true or false based on the input. I think you want parameterise here - that means to replace with a dynamic value. Hope that helps :-) Commented Aug 5, 2020 at 13:51
  • Thanks RichEdwards. For anyone else - the spaces of the " and & with respect to the variable are crucial but might be corrected by your code editor. Commented Aug 6, 2020 at 16:29

1 Answer 1

1

Try this in your loop:

ThisWorkbook.Sheets("Orders").Cells(r, 1).Value = bot.FindElementByXPath("//*[@id='ctl00_mainContent_ucOrdersList_dgOrders']/tbody/tr[" & r & "]/td[2]").Text

Use ampersand & to join strings.

This is the key bit: "...tr[" & r & "]/td..."

More info on joining strings in vb here

For the + operator, they state:

Although in principle the + sign is identical to the & concatenation operator, it also doubles as the addition operator. Hence, as Microsoft states:

When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.