简体   繁体   中英

How to select all class that is above certain tag using CSS Selector in Selenium + Python?

Here I want to get all the class = "result-row" which are above the "h4" tag, not the ones which are below "h4" tag.

在此处输入图片说明

My current code selects all of them:

section = driver.find_element_by_css_selector("[class='rows']")
result_rows = section.find_elements_by_css_selector("li.result-row")

so how can i get the desired result here?

You could try the following css which uses :not to filter out h4's general siblings based on class

li.result-row:not(h4.ban ~ li.result-row)

which might be simplified to:

.result-row:not(.ban ~ .result-row)

如果不需要CSS,则可以使用XPath:

driver.find_element_by_xpath("//h4/previous-sibling::li")

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