【问题标题】:Python - Selenium - Some elements found, but other notPython - Selenium - 找到了一些元素,但没有找到其他元素
【发布时间】:2021-04-29 20:56:22
【问题描述】:

找到前 10 个“td”,但没有找到其他 800 个左右。怎么会这样,很郁闷。

我只想要该页面上所有 TD 的列表,但即使所有内容都可见,也只能找到一些。

driver.get("http://www.skyracing.com.au/index.php?component=racing&task=todayraces&Itemid=88&id=18")
heading3 = len(driver.find_elements_by_xpath('//td'))
count = 0
while count <= heading3:
    count = count + 1
    StringCount = str(count)
    try:
        heading4 = driver.find_element_by_xpath("//td[" + StringCount + "]").text
    except NoSuchElementException:
        pass
    print(StringCount)
    print(heading4)

【问题讨论】:

  • 为什么不直接遍历driver.find_elements_by_xpath('//td')返回的列表?
  • 您是说heading3 以10 的长度返回吗?如果没有看到 HTML,很难说是什么导致了这种情况,但是如果 header3 的长度是 10,那么我猜你需要使用不同的 xpath。
  • 请提供html代码。这比为您的问题找到解决方案更容易。

标签: python selenium tags


【解决方案1】:

为什么不直接遍历 driver.find_elements_by_xpath('//td') 返回的列表?

for idx, elem in enumerate(driver.find_elements_by_xpath('//td')):
    print(i+1, elem.text)

【讨论】:

  • 感谢您指出这一点,但它并没有真正回答问题。
  • 您确定所有这些 TD 都在主文档中,而不是在一个或多个 IFRAME 元素中吗?
  • 或者您只需要等待其他 TD 加载即可。
猜你喜欢
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 2021-12-31
  • 2021-07-07
  • 2018-06-28
  • 2021-06-22
  • 2017-05-07
  • 1970-01-01
相关资源
最近更新 更多