1 回答

TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊
您可以使用 css 選擇器獲取相關(guān)的 li 項,如下所示:
通過css選擇器通過索引查找子元素
$(".content > ul > li:nth-child(2)").textContent >>> "Shipping Weight: 33 pounds (View shipping rates and policies)"
$(".content > ul > li:nth-child(3)").textContent >>> "ASIN: B07QKN2ZT9"
相關(guān)的python selenium代碼:
driver.find_element_by_css_selector(".content > ul > li:nth-child(3)").text.split(": ")[1] >>> 'B07QKN2ZT9'
通過 XPATH 查找祖先元素
如果 ASIN 并不總是在同一個索引中,那么您可以找到bdi具有文本文本的元素ASIN并找到它,ancestor::li然后獲取其文本并提取相關(guān)部分。像下面這樣:
driver.find_element_by_xpath("//bdi[text()='ASIN']/ancestor::li").text.split(": ")[1] >>> 'B07QKN2ZT9'
生成 XPATH
//<element type>[<attribute type> = <attribute value>]/<descendant>
//bdi[text() = 'ASIN'] >>> bdi element with text 'ASIN'
//bdi[@dir = 'ltr'] >>> bdi element with dir attribute equals to 'ltr'
訪問元素的祖先
/ancestor::<ancestor element type>
//bdi[text()='ASIN']/ancestor::li >>> li
//bdi[text()='ASIN']/ancestor::ul >>> ul
你可以檢查這個作為參考
添加回答
舉報