XPath to get text using sibling HTML element

I am having trouble getting my specific string from an ordered list. I am using C # and Visual Studio to search the website for a specific string and then import into an Excel sheet. In particular, this value must be the date of birth. My current query for a string looks like this:

driver.FindElement(By.XPath("//*[contains(text(), 'Birthdate')]")).Text;

      

The order list I am looking at looks like this:

<ol>                  
  <li>
        <label>Name</label>Humphries, Ryan</li>
    <li>
        <label>Birthdate</label>11/14/1992</li>
    <li>
        <label>SSN</label>

      

I can find "Birthdate" and I get this string to go back to my document with my current code, however I want the actual date of birth, not the label.

+3


source to share


1 answer


The problem is that you cannot find / reference the text node directly on selenium. A common way to solve this problem is to get the text of the parent element and "subtract" its child.

In other words, find the element li

, get the text, and replace with Birthdate

an empty string:



driver.FindElement(By.XPath("//li[label = 'Birthdate']")).Text.Replace("Birthdate", "")

      

+3


source







All Articles