【问题标题】:Get to second tr in data row table, Selenium Python获取数据行表中的第二个 tr,Selenium Python
【发布时间】:2018-03-04 23:26:30
【问题描述】:

我正在尝试获取体育数据。

allgames= driver.find_element_by_id("games")

box=allgames.find_element_by_link_text("Box Score")

这可行,但它只获取第一场比赛的得分(来自tr data-row="0"

我正在尝试找到一种方法可以转到tr data-row="1",然后使用.find_element_by_link_text("Box Score")

HTML 图片:

【问题讨论】:

标签: python selenium web-scraping


【解决方案1】:

您可以使用 xpath 或 css 来获取这些元素。请参阅 Selenium 的 examples 这些方法。

在你的情况下:

trs = allgames.find_elements_by_xpath("//tr")

这将为您提供存储在allgames 中的元素中的所有tr 元素。

【讨论】:

  • 以及如何到达 tr data-row="1"?我在每个 tr 数据行中寻找 .find_element_by_link_text("Box Score") ......我怎么称呼它
【解决方案2】:

有两种方法可以解决这个问题。

# find all tr tags within the game
tr_list = allgames.find_elements_by_tag_name('tr')
# select the Box Score
tr_list[1].find_element_by_link_text("Box Score")

另一种方法是按照@Alex 的建议使用 xpath,但要指定您想要的确切属性。

# find the tr tag within the game with data-row=1
tr = allgames.find_element_by_xpath('//tr[@data-row="1"]')
# select the Box Score
tr.find_element_by_link_text("Box Score")

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2021-08-21
    • 2012-11-23
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多