【问题标题】:Python selenium locate element in multiple iframesPython selenium 在多个 iframe 中定位元素
【发布时间】:2018-02-02 17:12:34
【问题描述】:

我是 Selenium for Python 的新手,我试图在多个 iframe 中定位元素。 这是我能看到的 DOM 元素。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>...</head>
    <body>
        <form>
        ...
           <div class="page">
               <div class="main clear" style="z-index: 20; position:relative;">
                   <div id="placeOrder">
                       <iframe src="BuyFlow.aspx" frameborder="0" width="1150" height="950">
                           #document
                               <html>
                                   <body>
                                       <form>
                                           ...
                                           <iframe id="CreativeLiftFrame">
                                               #document
                                                   <html>
                                                       ...
                                                       <body id="multiple-addresses">
                                                           ...
                                                       </body>
                                                   </html>
                                           </iframe>
                                        </form>
                                    </body>
                               </html>
                        </iframe>
                    </div>
                </div>
            </div>
        </form>
    </body>
</html>

我想要做的是获取第二个&lt;iframe&gt;&lt;body&gt; 标签的id 名称。

那是"multiple-addresses"

为了做到这一点,我编写了如下代码。

# Switch to the first iframe
iframe = driver.find_element(By.TAG_NAME, 'iframe')
driver.switch_to_frame(iframe)

# Fill in Address and ZipCode inputbox and submit form
address_input.send_keys(address)
postcode_input.send_keys(postcode)
postcode_input.send_keys(Keys.RETURN)

# Check Available - Inner iframe
second_iframe = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to_frame(second_iframe)
print(second_iframe.get_attribute("id")
body = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
print(body.get_attribute("id")

结果,我在控制台上得到了 2 个输出。

CreativeLiftFrame
None

如您所见,selenium 驱动程序定位到第二个iframe,但在第二个iframe 中找不到body 标签的id

我不知道如何处理它。

【问题讨论】:

  • 我在您的代码中看到两条打印语句和三行输出。产生错误的原因是什么?
  • 嗨,一个是例外。
  • 我明白了,但是什么命令产生了异常?
  • 我将脚本运行为 >>>python run.py。我的意思是上面的代码是sn-p,其他代码发生异常。
  • 您发布的脚本根本没有运行 ;)

标签: python selenium iframe selenium-webdriver


【解决方案1】:

一般来说,在 Selenium 中导航框架时,以下方法可能是最可靠的。

每次更改帧时,返回根帧,或默认:

driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//Some XPATH here')))
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//Some Xpath here')))

在您的情况下,切换到默认值,然后切换到第一个孩子,然后是下一个孩子,依此类推。在下一个帧切换时,重复此操作 - 首先是默认值,然后是第一个孩子,等等。

我还要添加您正在按 tag_name 搜索帧,这不是很具体。整个文档中有多少个带有该 tag_name 的标签?

如果该框架上确实没有唯一的idname,您可以使用这样的方式通过框架的src 进行搜索:

By.XPath("//iframe[contains(@src,'<src url here')]")

【讨论】:

    【解决方案2】:

    根据您共享的 HTML 来检索第二个孩子 &lt;frame&gt;body 标记的 id,您可以使用以下代码:

    # Switch to the first iframe
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='BuyFlow.aspx']")))
    
    # Fill in Address and ZipCode inputbox and submit form
    address_input.send_keys(address)
    postcode_input.send_keys(postcode)
    postcode_input.send_keys(Keys.RETURN)
    
    # Check Available - Inner iframe
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"CreativeLiftFrame")))
    print(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body'))).get_attribute("id"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多