使用包含和不包含的Python selenium xpath

2024-04-25 07:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试获取标题中包含一些单词的链接,但不包含一些单词,我使用以下代码,但它表示这不是有效的XPath表达式

请在此处找到我的代码:

任何帮助都将不胜感激

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

Tags: 代码inhttp标题forgettitle表达式
2条回答

xpath中有一个额外的括号,请使用

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

反而 您可以先使用chrome开发者工具来验证XPath enter image description here PS:为了能够在我的页面中找到一些元素,我在这里稍微修改了xpath

and之前应该有空格。在XPath中还有额外的前导括号。尝试:

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

相关问题 更多 >