我在 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
提交
取消