【问题标题】:Switch from one html tag to another using Xpath使用 Xpath 从一个 html 标签切换到另一个
【发布时间】:2014-11-21 13:02:27
【问题描述】:

这是我的 HTML 代码:

<pre>
<span class="cm-string">"userId"</span>
":"
<span class="cm-string">"abc"</span>
","
</pre>

我需要从“userId”标签中捕获文本“abc”,因为 abc 的值会不断变化。

我尝试了以下代码,但失败了。

driver.findElement(By.xpath("//span[contains(text(),'userId')]/span")).getText();

【问题讨论】:

  • 你的问题没有意义。没有userId 标签。 “userId”本身就是span 元素的文本内容。此外,您的 java 代码没有任何意义,因为您选择了一个包含某个字符串的 span,然后...返回此字符串?

标签: java html selenium xpath selenium-webdriver


【解决方案1】:

包含abcspan 是包含UserIdspan 的以下兄弟,使用它:

//span[contains(., 'userId')]/following-sibling::span

【讨论】:

  • 另一个疑问,有许多名为“userId”的子字符串我如何找到确切的字符串“userId”?下面的代码由于 cotes //span[contains(., '"userId"')]/following-sibling::span 而给出错误
  • @user3174553 当然,只是转义引号,不要使用contains()://span[. = '\"userId\"']/following-sibling::span
  •  "userId" ":" "abc" ", " 
     "密码" ":" "xyz" ","  pre> 在上面的代码中,我如何从 abc 访问到 xyz。如果你能帮助我完成最后的代码,我将非常感激。
猜你喜欢
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2016-04-19
  • 2011-10-28
  • 2020-10-25
  • 2017-05-03
  • 1970-01-01
相关资源
最近更新 更多