【问题标题】:How to get the value inside <span> element of an iframe如何获取 iframe 的 <span> 元素内的值
【发布时间】:2019-08-02 01:22:35
【问题描述】:

我有两个名为 Test1 和 Test2 的 iframe,在每个 iframe 中都有以下代码 sn-p。我想在 .

<div class="Test-font"> 
  <h6 class="Test_something d-inline">Available MQ Connections: </h6>
  <span class= "d-inline" id="availcons">24</span>
</div>

我使用了下面的代码。我得到了第一个 Test1 的值,但我没有得到第二个 iframe 的任何结果。甚至没有错误。

driver.switch_to.frame(driver.find_element_by_id("Test1"))
Test_1_connections = driver.find_element_by_id("availcons").text

driver.switch_to.default_content()

driver.switch_to.frame(driver.find_element_by_id("Test2"))
Test_2_connections = driver.find_element_by_class_id("availcons").text

我希望输出为 24,但我得到的只是黑屏。

【问题讨论】:

标签: python html selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试使用显式等待 iframe 的可用性,然后在其中执行操作:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


ui.WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "Test1")))

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 2013-12-21
    • 2012-11-03
    • 2013-01-27
    • 2016-09-02
    • 2020-08-02
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多