gpt4 book ai didi

python - 查找以文本长度为条件的特定 xpath Python Selenium

转载 作者:太空宇宙 更新时间:2023-11-04 05:03:41 26 4
gpt4 key购买 nike

为了在很多很多页面中获得我想要的正确内容 - 我想出了一个在 99% 的时间都有效的规则:

//a[@class='popular class' and not (contains(text(),'text1')) and not (contains(text(),'text2'))]

另外 1% 导致找到超过 1 个匹配节点,需要对 "not (contains(text(), 'specialtext')) 进行更多特殊处理

我想通了,我想要的值只有 1 个字符,最多 4 个字符,而特殊处理情况总是会超过 4 位数字。

我想做的是在我的 xpath 中添加另一个条件,如下所示:

//a[@class='popular class' and not (contains(text(),'text1')) and not (contains(text(),'text2')) and (text_length() < 5)]

这应该作为一个 100% 的规则工作,它将始终为我提供我需要的特定节点。我在 Java 中看到过类似的东西:

System.out.println("Select elements which 
have string length less than 4");
expr = xpath.compile(".//*[string-length(name()) < '4']")

(From this website)

但找不到与 Python 匹配的函数。

有没有?

最佳答案

string-length()不是 Java 函数,而是 XPath 函数!您可以在表达式中以相同的方式使用:

//a[@class='popular class' and not (contains(text(),'text1')) and not (contains(text(),'text2')) and string-length(.) < 5]

关于python - 查找以文本长度为条件的特定 xpath Python Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45059380/

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