[英]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. 在这里,我要获取所有在“ h4”标签上方的类=“结果行”,而不是在“ h4”标签下方的所有类。
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 您可以尝试使用以下css,它使用:not根据类过滤掉h4的常规兄弟
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.