0

My question relates to coding selenium specifically in VBA. I have a function that finds webelements based on text passed to it (in string variable 'toFind'). The relevent Xpath identification method I use is (where driver. is the selenium chromedriver):

mySearch = "//*[contains(text(),'" & toFind & "')]"
Set ret = driver.FindElementsByXPath(mySearch)

This works unless the toFind variable contains an apostrophe. For example if "Consultant's Forename" is passed then my expression evaluates to: Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant's Forename')]"), which causes an invalid selector run-time error.

I have researched elsewhere on the site and see a number of answers describing escaping from the single quotes using the backslash character. Based on this I have tried to use Set ret = driver.FindElementsByXPath("//*[contains(text(),\"Consultant's Forename\")]") instead. However, this will not compile in microsoft visual basic for applications as it reports a syntax error (code line is red). I have not tried using the driver.findElements(By.xpath method as opposed to driver.FindElementsByXPath as I assumed this would not make a difference to the handling of the XPath expression. I have tried the other suggestions of using the 'concat' function but this also seems not to be valid in VBA selenium.

I don't know if these methods are specficaly for platforms other than VBA or I am just getting my syntax wrong?

The only way I can work it at present is to ignore the existence of the apostrophe:

Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant') and contains(text(),'s Forename')]")

Whilst this works it is an incomplete solution and any help on the correct syntax to deal with the xpath location in VBA for text containing an apostrophe would be much appreciated.

Saqib
  • 43
  • 8
  • 1
    You could try to escape as follows `replace$(toFind, chr$(39), chr$(92)& chr$(39))` Or try doubling the quotes `Set ret = driver.FindElementsByXPath("//*[contains(text(),""Consultant's Forename"")]")` – QHarr Mar 09 '22 at 00:16
  • @QHarr - thanks for the reply. Unfortunately neither work. `replace$(toFind, chr$(39), chr$(92)& chr$(39))` produces the same run time error. `Set ret = driver.FindElementsByXPath("//*[contains(text(),""Consultant's Forename"")]")` won't compile due to syntax error in VBA. – Saqib Mar 09 '22 at 09:21
  • What is the exact error message with the second suggestion please? – QHarr Mar 09 '22 at 17:19
  • What about the special character entity `'` instead of the inner apostrophe: `Set ret = driver.FindElementsByXPath("//*[contains(text(),'Consultant's Forename')]")` ? @Saqib – T.M. Mar 09 '22 at 18:02
  • @T.M. - Unfortunately `'` did not work – Saqib Mar 09 '22 at 21:00
  • 1
    @QHarr - The doubling quotes method works! I must have made a transcription error before. Thanks again for you help – Saqib Mar 09 '22 at 21:09
  • Thanks to @QHarr for the answer. Below is the adaptation to my original code that will now work in VBA Selenium with string's that contain an apostrophe as well as those that don't: `toFind = "Consultant's Forename": mySearch = "//*[contains(text(),""" & toFind & """)]": Set ret = driver.FindElementsByXPath(mySearch)` – Saqib Mar 10 '22 at 09:51

0 Answers0