如何获取父元素文本并删除子元素文本 selenium c#? [英] How to get parent element text and remove child element text selenium c#?

查看:33
本文介绍了如何获取父元素文本并删除子元素文本 selenium c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是自动化测试的新手,现在我正在使用 selenium C#.我有一个问题,我想从一个元素中获取文本,但是代码:

文本1<span>textdontwant</span>

和我的声明

<块引用>

driver.FindElement(By.XPath("///*[@id='contactList']/div[1]/div/div[1]/div/div/div/div[2]")).文字;

获取:

<块引用>

Text1textdontwant

我只需要父元素文本是:

<块引用>

文本 1

有人有解决办法吗?非常感谢!这里是 HTML:

<div style="溢出:可见;高度:0px;宽度:0px;"><div class="virtualized-scroll testing" style="height: 657px; width: 304px;"><div aria-label="grid" class="ReactVirtualized__Grid ReactVirtualized__List" role="grid" tabindex="-2" style="box-sizing: border-box; direction: ltr; height: 657px; position: absolute;宽度:304px;将改变:自动;溢出:自动;"><div class="ReactVirtualized__Grid__innerScrollContainer" style="width: auto; height: 2198px; max-width: 304px; max-height: 2198px; overflow: hidden; position: relative;"><div style="height: 44px; left: 0px; position: absolute; top: 0px; width: 100%;"><div class="contact-list__add flx flx-al-c nav-button_add_friend 可点击"><i class="fa fa-icon-add-friend contact-list__add__icon"></i><spanclass="contact_add_friend_item" data-translate-inner="STR_CONTACT_ADD_FRIEND">Thêm bạn</span></div>

<div style="height: 72px; left: 0px; position: absolute; top: 266px; width: 100%;"><div class="item 最小化 rel list-friend-conctact"><div class="avatar avatar--m list-friend-conctact__avatar"><div class="avatar-img outline" style="background-image: url("//s120.avatar.talk.zdn.vn/4/8/c/f/27/120/c5ee9f2b5a5a87d227fe2faf376f2e2c.jpg&quot;);"></div>

<div class="list-friend-conctactfriend_onlines_dot"></div><div class="item-title list-friend-conctact__title has-status ">Text1<span class="friend_online_status truncate" style="top: 0px; left: 3px;">文字不想要</span>

解决方案

根据您提供的用于提取文本 Text1 的 HTML,您可以使用以下代码块:

IWebElement myElem = driver.FindElement(By.XPath("//div[@class='item-title list-friend-conctact__title has-status']"));string myText = (string)((IJavaScriptExecutor)driver).ExecuteScript("返回参数[0].firstChild.textContent;", myElem);

i'm a newbie of automation testing, now i'm using selenium C#. I have a problem, i want to get text from a element, but the code :

<div class="item">
Text1
<span>textdontwant</span>
</div>

and my statement

driver.FindElement(By.XPath("//*[@id='contactList']/div[1]/div/div[1]/div/div/div/div[2]")).Text;

Get:

Text1textdontwant

I need only parent element text is :

Text1

Anyone have solution? Thanks so much! here HTML:

<div id="contactList" class="web z-data-list" tabindex="30" style="position: relative;">
   <div style="overflow: visible; height: 0px; width: 0px;">
      <div class="virtualized-scroll testing" style="height: 657px; width: 304px;">
         <div aria-label="grid" class="ReactVirtualized__Grid ReactVirtualized__List" role="grid" tabindex="-2" style="box-sizing: border-box; direction: ltr; height: 657px; position: absolute; width: 304px; will-change: auto; overflow: auto;">
            <div class="ReactVirtualized__Grid__innerScrollContainer" style="width: auto; height: 2198px; max-width: 304px; max-height: 2198px; overflow: hidden; position: relative;">
               <div style="height: 44px; left: 0px; position: absolute; top: 0px; width: 100%;">
                  <div class="contact-list__add flx flx-al-c nav-button_add_friend clickable  "><i class="fa fa-icon-add-friend contact-list__add__icon "></i><span class="contact_add_friend_item" data-translate-inner="STR_CONTACT_ADD_FRIEND">Thêm bạn</span></div>
               </div>


             <div style="height: 72px; left: 0px; position: absolute; top: 266px; width: 100%;">
   <div class="item minimized rel list-friend-conctact  ">
      <div class="avatar avatar--m list-friend-conctact__avatar">
         <div class="avatar-img  outline" style="background-image: url(&quot;//s120.avatar.talk.zdn.vn/4/8/c/f/27/120/c5ee9f2b5a5a87d227fe2faf376f2e2c.jpg&quot;);"></div>
      </div>
      <div class="list-friend-conctact friend_onlines_dot"></div>
      <div class="item-title list-friend-conctact__title has-status ">Text1
<span class="friend_online_status truncate" style="top: 0px; left: 3px;">Text dont want</span>
</div>
</div>


   </div>
</div>

解决方案

As per the HTML you have provided to extract the text Text1 you can use the following code block:

IWebElement myElem = driver.FindElement(By.XPath("//div[@class='item-title list-friend-conctact__title has-status']"));
string myText = (string)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].firstChild.textContent;", myElem);

这篇关于如何获取父元素文本并删除子元素文本 selenium c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆