【问题标题】:Xpath expression with both an ID and contains Text具有 ID 并包含文本的 Xpath 表达式
【发布时间】:2018-08-09 07:32:34
【问题描述】:

所以我想使用 Xpath 找到以下标记:

 <h1 id="label">This is the text</h1>

通常,我会这样写:

//h1[@id='Label' and text() = "This is the text"]

或者,如果我只想提取部分文本,我使用以下内容:

//h1[text()[contains(.,'text')]]

现在,我试图寻找可以混合使用两种语句的教程:所以我想要一个包含@id 和文本“Text”的 Xpath 表达式。 这样的事情甚至可能吗?

我试过这样做:

//h1[@id='Label' and [contains(@text,"text"])] 

但这并没有给我一个正确的结果。

【问题讨论】:

  • 我经常使用“包含”,但还有更多。下面是一些例子: 多个条件: //h1[@id='Label' and contains(text(), 'text')] 部分匹配: //span[contains(text(), 'Assign Rate')] 开始-with: //input[starts-with(@id,'reportcombo');值有空格://div[./div/div[normalize-space(.)='More Actions...']] 兄弟://td[.='LoadType']/following-sibling::td[ 1]/select" 更复杂://td[contains(normalize-space(@class), 'actualcell sajcell-row-lines saj-special x-grid-row-collapsed')]
  • @Andersson 如果您根据上述粒度级别重复锤子问题,则每个问题都与至少带有selenium 标记的现有问题重复。我们可以重新提出这个问题吗?
  • @DebanjanB ,这个问题对社区没有好处:如果它不是重复的,那么它应该被关闭为“简单的印刷错误”:注意 OP 错误地使用了右括号/方括号。如果那个错字修正了... OP 应该检查how to use multiple predicates
  • @Andersson 暂时让我们把多谓词理论的高级话题分开。 OP是新用户。我们的努力应该是为新用户提供有效的体验。正如您正确指出的Simple typographical error,新用户期望得到帮助的地方。也许 dup 锤在这里很苛刻。
  • @DebanjanB ,实际上,第一个帮助是 OP 得到的语法错误的回溯,并且 Andrei Suvorkov 已经提供了正确的答案。好的,如果你有什么要补充的 - 重新打开

标签: java selenium xpath webdriver


【解决方案1】:

替换:

//h1[@id='Label' and [contains(@text,"text"])] 

与:

//h1[@id='label' and contains(text(),'text')] 

【讨论】:

  • 请注意,OP 的 XPath 在语法上不正确,您只是复制了其中一个错误:将 @text 替换为 text()
猜你喜欢
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2022-10-03
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多