简体   繁体   English

Python selenium xpath 使用包含和不包含

[英]Python selenium xpath using contains and not contains

I try to get links whose title contains some word in the mean time not contains some words, I use the following code but it says is not a valid XPath expression.我尝试获取标题包含某些单词同时不包含某些单词的链接,我使用以下代码,但它说不是有效的 XPath 表达式。

Please find my code here:请在这里找到我的代码:

Any help will be highly appreciated!任何帮助将不胜感激!

driver.get("http://www.csisc.cn/zbscbzw/isinbm/index_list_code.shtml")
while True:
    links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[(contains(@title,'公司债券')and not(contains(@title,'短期'))]")]
    for link in links:
               driver.get(link)
               #dosth

There is an extra bracket in you xpath, use你有一个额外的支架 xpath,使用

links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[contains(@title,'公司债券')and not(contains(@title,'短期'))]")] 

instead You can use chrome developer tools first to validate your xpaths相反,您可以先使用 chrome 开发人员工具来验证您的 xpath 在此处输入图像描述 PS: I changed the xpath here a bit to be able to find some elements in my page PS:我在这里更改了 xpath 以便能够在我的页面中找到一些元素

There should be space before and . and之前应该有空格。 Also there is extra leading bracket in your XPath.您的 XPath 中还有额外的前导括号。 Try:尝试:

"//a[contains(@title,'公司债券') and not(contains(@title,'短期'))]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM