【问题标题】:How to write a locator and get both the text present in a single tag in HTML code for selenium automation如何编写定位器并获取 HTML 代码中单个标签中的文本以实现硒自动化
【发布时间】:2021-05-03 00:55:49
【问题描述】:

这是我的html代码

<h2>==$0
"How"
 "Are You"
</h2>

编写定位器如//h2/text()[1] 获取“如何”文本并编写//h2/text()[2] 获取“你是”文本对我不起作用。抛出以下错误

onException - WebDriver error:
<font color= 'red' size='3.5'>org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//h2/text()[2]" is: [object Text]. It should be an element.

【问题讨论】:

    标签: java html selenium


    【解决方案1】:

    text() 不返回 webelement 节点,而是返回文本内容

    改用

    //*[text()[contains(.,"Are You")]]
    

    现在找到文本为

    elem.text()
    

    你必须使用字符串运算符来提取你想要的特定文本

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 2020-05-26
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      相关资源
      最近更新 更多