简体   繁体   English

如何在Selenium + Python中使用CSS选择器选择特定标记之上的所有类?

[英]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.

相关问题 如何使用css_selector选择硒python? - How to select with css_selector selenium python? Python:使用Selenium查找所有特定标签 - Python: find all certain tag by using selenium 如何通过CSS选择器选择标签 - How to select a tag by css selector 如何在 selenium python 中通过 CSS Selector 找到确切的类? - How to find the exact class by CSS Selector in selenium python? 如何使用 css 选择器或任何其他定位器 selenium python 查找具有特定类属性的元素 - how to find element with specific class attributes using css selector or any other locator selenium python 如何使用python /硒通过CSS选择器查找元素 - how to find element by css selector using python / selenium 如何使用 selenium python 通过文本单击 css 选择器 - How to click css selector by text using selenium python 如何在Python Selenium中使用xpath或CSS选择器查找元素 - How to find element using xpath or css selector in Python Selenium 如何在Selenium的XPath选择器中选择所有子文本,但不包括标签? - How to select all children text but excluding a tag in Selenium's XPath selector? 如何选择一个<option>在一个<select>在 Python 3 中使用 Selenium 标记? - How to select an <option> within a <select> tag using Selenium in Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM