我在 Python 項目中使用 Selenium,我想使用以下代碼獲取表中的數據:xpath = "//div[@id='tab_highscore']//table[contains(@class, 'highscore')]//tr"users = driver.find_elements_by_xpath(xpath)for u in users: xpath = "//td[@class='name']" userTd = u.find_elements_by_xpath(xpath)[0] user = userTd.get_attribute('innerText') xpath = "//td[@class='score']" scoreTd = u.find_elements_by_xpath(xpath)[0] score = scoreTd.get_attribute('innerText') usersArr.append({"name":user, "ally":ally, "score":score})在用戶和分數中我總是得到第一個 tr 值的問題,知道問題是什么嗎?
1 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
您需要指定指向每次迭代的特定上下文節點(XPath 表達式開頭的點) :u
for u in users: xpath = ".//td[@class='name']" userTd = u.find_element_by_xpath(xpath) ...
PS 使用find_element_by_xpath(xpath)
代替find_elements_by_xpath(xpath)[0]
和.text
代替.get_attribute('innerText')
添加回答
舉報
0/150
提交
取消