【问题标题】:python selenium find element by tag inside a div (has no name, class, id or text)python selenium 在 div 中按标签查找元素(没有名称、类、id 或文本)
【发布时间】:2021-04-28 17:24:42
【问题描述】:

我正在尝试使用 selenium 获取 iframe 的 src,但我找不到如何在 div 中获取 iframe(div 有一个 id)。这是我所拥有的:

<div role="tabpanel" class="tab-pane active" id="video_box" style="background: #000; color: #fff;">
  <iframe width="560" height="315" frameborder="0" src="https://embedsito.com/v/x431gb5q0rq76j-" scrolling="no" allowfullscreen=""></iframe>
</div>

如您所见,我可以使用 driver.find_element_by_id("video_box") 通过 id 获取 div,但我需要的是 div 内部的 src,它没有类名、没有 id、没有名称和文本(好吧,它实际上有来自 src 的文本,但它每次都会改变)。有没有办法从 div 中获取 iframe 并从中提取 src?

我尝试使用find_by_css_selector('iframe') 方法但没有结果 我尝试使用find_element_by_tag_name('iframe') 方法,但引发异常:

selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:iframe

【问题讨论】:

    标签: python html selenium iframe


    【解决方案1】:

    您是否尝试过进入开发人员工具(检查元素)并通过右键单击 iframe 来获取 iframe 的 XPath?

    然后你可以使用命令find_element_by_xpath()方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 2015-07-12
      • 2019-11-18
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多