css/xpath选择器,用于在使用Selenium WebDriver(java)时排除元素中的子节点 [英] css/xpath selector to exclude the child node in the element when using selenium webdriver (java)

查看:185
本文介绍了css/xpath选择器,用于在使用Selenium WebDriver(java)时排除元素中的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS/xpath选择器以获取链接文本,但不包括.muted中的文本.

CSS/xpath selector to get the link text excluding the text in .muted.

我有这样的html:

<a href="link">
          Text
   <span class="muted"> –text</span>
</a>

当我执行getText()时,会得到完整的文本,例如Text-text.是否可以排除静音的子类文本?

When I do getText(), I get the complete text like, Text-text. Is it possible to exclude the muted subclass text ?

尝试cssSelector = "a:not([span='muted'])"不起作用.

xpath = "//a/node()[not(name()='span')][1]"

错误:xpath表达式"//a/node()[not(name()='span')][1]"的结果是:[objectText].它应该是一个元素.

ERROR: The result of the xpath expression "//a/node()[not(name()='span')][1]" is: [objectText]. It should be an element.

推荐答案

AFAIK仅使用CSS选择器无法完成此操作.您可以尝试使用JavaScriptExecutor来获取所需的文本.

AFAIK this cannot be done with CSS selector only. You can try to use JavaScriptExecutor to get required text.

由于您没有提到编程语言,因此在Python上为您展示了示例:

As you didn't mention programming language you use I show you example on Python:

link = driver.find_element_by_css_selector('a[href="link"]')
driver.execute_script('return arguments[0].childNodes[0].nodeValue', link)

这将仅返回"Text"而没有" -text"

这篇关于css/xpath选择器,用于在使用Selenium WebDriver(java)时排除元素中的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆