Search code examples
excelvbaselenium-webdriverxpathwebdriver

Selenium extract data after </strong>


I'm tying to extract the phone data after a </strong> in this web page code:<strong class="styles__SCSectionTitleTitle-sc-v0nzb6-5 hCjKsp">Teléfonos</strong>669794282

Using the xpath OBJ.FindElementByXPath("//*[@id='main']/div[1]/div[2]/div[2]/div/div/div/div/div/section/div/div[1]/div[2]/ul/div/div[3]/li/div/div//strong").Text, I only get the word "Teléfonos".

Any help, please?

Thanks in advance,

code_image


Solution

  • First identify the parent element div of child strong have a text Teléfonos

    Then use the ExecuteScript to get the node values.

    OBJ.ExecuteScript('return arguments[0].childNodes[1].textContent;', OBJ.FindElementByXPath("//div[./strong[text()='Teléfonos']]"))
    
    OBJ.ExecuteScript('return arguments[0].childNodes[2].textContent;', OBJ.FindElementByXPath("//div[./strong[text()='Teléfonos']]"))
    
    OBJ.ExecuteScript('return arguments[0].childNodes[3].textContent;', OBJ.FindElementByXPath("//div[./strong[text()='Teléfonos']]"))
    
    OBJ.ExecuteScript('return arguments[0].childNodes[4].textContent;', OBJ.FindElementByXPath("//div[./strong[text()='Teléfonos']]"))
    

    Alternatively you can use regex to extract the number value from a string.

    parentDivText=OBJ.FindElementByXPath("//div[.//strong[text()='Teléfonos']]").Text
    Dim re As Object
    Dim PhoneNumbers
    
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "\d{9}"
    re.IgnoreCase = True
    re.Global = True
    
    Set PhoneNumbers = re.Execute(parentDivText)
    PhoneNumber=PhoneNumbers(1)