【问题标题】:Can't get span text using csspath无法使用 csspath 获取跨度文本
【发布时间】:2014-04-25 23:12:03
【问题描述】:

我在 selenium c# 中使用 csspath 查询 span 元素(没有类、id 或任何东西),但 text 属性为空。

发生了什么,我怎样才能在 span 中获取文本?

所以DOM结构是:

/div/div/ul/li/a/span

查询是:

driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span"));

【问题讨论】:

  • 好吧,我不知道你的 html 是什么,所以我怎么知道你的选择器(似乎主要是类)是否准确
  • 你的DOM结构中有6个嵌套元素,CSS选择器中有8个嵌套元素,显然有问题,你最好显示html。
  • 那里我上传了html..谢谢!
  • 您应该看看是否可以将 HTML 作为文本抓取到此处发布,而不是上传屏幕截图。

标签: c# selenium css-selectors


【解决方案1】:

你可以试试

driver.FindElement(By.XPath("//div[@id='otherOuterDiv']//span"));

我通常使用相对 XPath,因为我发现指定它们更容易。

【讨论】:

    【解决方案2】:

    解决办法是:

    string text = driver.FindElement(By.CssSelector(".contactActions .iconActions .icon .privacy span")).GetAttribute("textContent"));

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多