從下面的 html 中,我想在 div1 和 div 2 之間獲取 div,我需要幫助找出一種可以使用 selenium 獲取 div 的方法
<div class="div"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div class="div2"></div>
這是使用的代碼
driver.find_element_by_xpath('.//div[@class="div1"]').find_elements_by_xpath('//following::div')
uj5u.com熱心網友回復:
如果你想得到之間的div
div class div1
和
div class div2
請使用下面的XPath
.//div[@class='div1']//following-sibling::div[not(@class='div2')]
在你的代碼中,是這樣的:
driver.find_element_by_xpath('.//div[@]').find_elements_by_xpath(".//div[@class='div1']//following-sibling::div[not(@class='div2')]")
為了測驗這個,我創建了一個虛擬的 HTML,

如您所見,它突出顯示了 4 個節點。
相信這會有所幫助!
uj5u.com熱心網友回復:
在你的情況下 css 選擇器將是一個更好的解決方案:
const cssSelector = ".div ~ div:not(.div2 ~ *):not(.div2)"
基本上它的作用是:
.div ~ // selects siblings
div:not(.div2 ~ *):not(.div2) // selects all div tags expect ones that are after .div2 and .div2 itself
您可以嘗試使用:
document.querySelectorAll(".div ~ div:not(.div2 ~ *):not(.div2)");
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/350233.html
