I have having trouble getting my specific string from an ordered list. I am using C# and Visual Studio to search a website for a specific string to then import into an Excel sheet. Specifically, this value needs to be a birthdate. My current request for the string is as follows:
driver.FindElement(By.XPath("//*[contains(text(), 'Birthdate')]")).Text;
The Ordered list that I am searching through is as follows:
NameHumphries, Ryan
Birthdate11/14/1992
SSN
I can search for 'Birthdate' and I get that string to return to my document with my current code, however, I want the actual birthdate, not the label.
Solutions1
The problem is, you cannot find/refer to a text node with selenium directly. The common way to approach this problem is to get the text of the parent element and "subtract" the child element's text from it.
In other words, find the li element, get the text and replace Birthdate with an empty string:
driver.FindElement(By.XPath("//li[label = 'Birthdate']")).Text.Replace("Birthdate", "")
Talk1:
Good solution alecxe
Talk2:
Thank you for such a quick response! Specifically, my code is as follows: activesheet.Range["B" + n].Value = driver.FindElement(By.XPath("//li[label = 'Birthdate]")).Text.Replace("Birthdate", ""); Your solution does help me, but the area in my Excel sheet remains blank. I am just a bit puzzled as to why. Any suggestions? Thank you! I apologize for formatting, I am new to the site.
Talk3:
thank you, can you print out the result of driver.FindElement(By.XPath("//li[label = 'Birthdate]")).Text.Replace("Birthdate", "") on the console and see what the value is going to be?
Talk4:
I created a new variable, String DOB, with messagebox() outputting a blank window when I tell it to show me DOB, So, I am under the assumption that is is blank and/or null. DOB = activesheet.Range["B" + n].Value = driver.FindElement(By.XPath("//li[label = 'Birthdate']")).Text.Replace("Birthdate", "");
Talk5:
Side note: changing HTML may be better solution as it would allow easier styling of the text in addition to simplifying the search:
- Birthdate11/14/1992.