簡體   English   中英

Python selenium xpath 使用包含和不包含

[英]Python selenium xpath using contains and not contains

我嘗試獲取標題包含某些單詞同時不包含某些單詞的鏈接,我使用以下代碼,但它說不是有效的 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

你有一個額外的支架 xpath,使用

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

相反,您可以先使用 chrome 開發人員工具來驗證您的 xpath 在此處輸入圖像描述 PS:我在這里更改了 xpath 以便能夠在我的頁面中找到一些元素

and之前應該有空格。 您的 XPath 中還有額外的前導括號。 嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM