gpt4 book ai didi

selenium-webdriver - Selenium:有没有一种方法可以编写xpath来获取包含以数字结尾的文本的元素

转载 作者:行者123 更新时间:2023-12-03 17:21:04 25 4
gpt4 key购买 nike

我正在尝试编写一个xpath,以选择所有包含以数字结尾并用大括号括起来的文本的元素。这些数字可以是任何数字,也可以是任何数字。在XPath1.0中可以吗?我不能使用正则表达式,因为XPath1.0不允许使用

<div class = '12345' >
<div class = '98231'>I want this (101)</div>
<div class = '98232'>I don't want this 101</div>
<div class = '98233'>I want this (1)</div>
<div class = '98234'>I don't want this (10A1)</div>
</div>

最佳答案

在XPath-1.0中,您不能使用RegEx。
但是您可以使用fn:number()函数使用fn:boolean函数检查给定的字符串是否为有效数字:

/div/div[boolean(number(substring-before(substring-after(.,'('),')')))]


显然,这仅在字符串中没有其他方括号的情况下有效。并且它不会检查括号中的数字是否在字符串的末尾。要添加此进一步的限制,请将表达式更改为

/div/div[not(boolean(string(normalize-space(substring-after(.,')'))))) and boolean(number(substring-before(substring-after(.,'('),')')))]


这会添加一个进一步的谓词,以检查 )之后是否存在除空格以外的其他字符。括号之间的前导空格和尾随空格由 fn:number()功能自动删除。

关于selenium-webdriver - Selenium:有没有一种方法可以编写xpath来获取包含以数字结尾的文本的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com