简体   繁体   中英

find_elements_by_xpath that do not contains 3 times a specific string

I have used this code but I want to remove those links where it appears the string 'vs' three times in the link, it only has to appear one time:

elems = driver.find_elements_by_xpath("//a[contains(@href, '/president/us/general_election')][not(@href = following::a/@href)]")
for elem in elems:
    print(elem.get_attribute("href"))

在此处输入图片说明

Update : I have realised that some of my code is not working as I expected, I used the code : [not(@href = following::a/@href)] to remove repeated href but there is still one href that is repeated. Any help is welcome!

Try this:

elems = driver.find_elements_by_xpath("//a[contains(@href, '/president/us/general_election')][not(@href = following::a/@href)]")
for elem in elems:
    if elem.get_attribute("href").count("vs") == 1: print(elem.get_attribute("href"))

To get link with only one occurrence of "_vs_" in @href you can extend your XPath with predicate

[not(contains(substring-after(@href, "_vs_"), "_vs_"))]

Your final XPath will be

"//a[contains(@href, '/president/us/general_election') and not(contains(substring-after(@href, "_vs_"), "_vs_")) and not(@href = following::a/@href)]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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