請建議我如何有效地更改 tr[1]、[2]、[3] 或 N 個數字,按照網站上提供的表格自動進行
代碼 :
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[1]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[2]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[3]/td[9]').click()
uj5u.com熱心網友回復:
您可以考慮使用find_elements_by_xpath('.//tr')代替find_element_by_xpath.
或者,您可以使用帶有 try-except 的簡單 for 回圈。它不是最優雅的,但由于例外,它會在出門前為您提供所有 N 行。
uj5u.com熱心網友回復:
嘗試如下:
options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr') # this xpath should highlight all the tr tags in the DOM
for opt in options:
opt.find_element_by_xpath('./td[9]') # this should get the 9th td tag of that particular tr. Use a "." in the xpath to find element within an element.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338315.html
上一篇:從抓取的資料中洗掉頁眉和頁腳部分
