html event 获取同级元素,XPath通过同级HTML元素获取文本

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:

  1. NameHumphries, Ryan

  2. Birthdate11/14/1992

  3. 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:

  4. Birthdate11/14/1992.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值