在 selenium web 驱动程序中如何选择正确的 iframe [英] in selenium web driver how to choose the correct iframe

查看:40
本文介绍了在 selenium web 驱动程序中如何选择正确的 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试单击此页面上的元素:

I'm trying to click on an element on this page:

url = 'https://finance.yahoo.com/quote/GOOG?ltr=1'
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_link_text('Financials')

此时我想点击现金流"、资产负债表"或季度".我知道这些按钮已被加载,因为我可以使用 BeautifulSoup 从页面源中提取它们.但是当我尝试使用 Selenium 来做到这一点时:

At this point I would like to click on 'Cash Flow', 'Balance Sheet', or 'Quarterly'. I know these buttons have been loaded because I can extract them using BeautifulSoup from the page source. But when I try to do it using Selenium:

driver.find_element_by_link_text('Cash Flow')
driver.find_element_by_link_text('Balance Sheet')
driver.find_element_by_link_text('Quarterly')

所有返回无法定位元素",除了季度"返回一个元素,但它是位于图表上方的元素,而不是我感兴趣的表格上方的元素.

All return 'Unable to locate element' except for 'Quarterly' which returns an element but its the one sitting above the graph and not the one above the table which is what I'm interested in.

我认为这是由于在错误的 iframe 中,并且我已找到所有 iframe:

I think this is due to being in the wrong iframe, and I have located all iframes:

driver.find_elements_by_tag_name('iframe')

返回 9 个元素.但是我无法确定要单击的元素属于哪个 iframe.我也按顺序浏览了 iframe,但仍然找不到我感兴趣的元素.

which returns 9 elements. But I'm having trouble figuring which iframe the elements I want to click on belong to. I also went through the iframes sequentially and still couldn't find the elements I'm interested in.

推荐答案

我刚刚查看了网站,它们(您正在寻找的元素)不在任何 iframe强> 标签.

I just checked in the website, they (the elements you are looking for) are NOT in any iframe tag.

以下代码对我有用(更改为 xpath,无需切换):

Following code worked for me (changed to xpath, no need to switch):

driver.find_element_by_xpath("//span[contains(text(),'Cash Flow')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Balance Sheet')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Quarterly')]").click()

注意:可能是因为对于财务",父标签是 a 表示链接,但对于其他元素(现金流量,资产负债表), 父标签是 div ,它不是链接标签.所以 find_element_by_link_text 可能没有用.

Note: It might be the reason that for "Financials", parent tag is a which represent a link, but for other elements (Cash Flow, Balance sheet), parent tag is div which is not a link tag. so find_element_by_link_text might not have been worked.

在 iframe 之间切换:

在我们尝试识别元素之前,您必须切换到元素所在的框架.

让我们假设,您的元素位于 3 个 iframe 内,如下所示:

Lets assume, your element is inside 3 iframes as follows:

<iframe name="frame1">
  <iframe name="frame2">
     <iframe name="frame3">
        <span>CashFlow</span> <! Span element is inside of 3 iframes>
    </iframe>
    <span>balance sheet> <! Span element is inside of 2 iframes>
  </iframe>
</iframe>

现在,如果您想确定三个 iFrame 中的 CashFlow:

Now, if you want to identify CashFlow which is inside the three iFrames:

    driver.switch_to_frame(driver.find_element_by_name("frame1")) // here, you can provide WebElement representing the iFrame or the index.
    driver.switch_to_frame(driver.find_element_by_name("frame2"))
    driver.switch_to_frame(driver.find_element_by_name("frame3"))
    driver.find_element_by_link_text("CachFlow")

    # switch to default frame before you again try find some other element which is not in the same frame (frame3)

   driver.switch_to_default_content()

   # then navigate to the frame that you want to indentify the element:
   driver.switch_to_frame(driver.find_element_by_name("frame1")) 
   driver.switch_to_frame(driver.find_element_by_name("frame2"))
   driver.find_element_by_link_text("balance sheet")

  # switch to default content again
  driver.switch_to_default_content()

注意:我使用了帧引用而不是索引,正如您提到的,有 9 个 iFrame.因此,使用索引会令人困惑.如果您无法识别 frameElement,则只能使用索引.

Note: I used Frame References instead of indexes as you mentioned there are 9 iFrames. so, using indexes would be confusing. If you can't identify frameElement, then only go for indices.

参考:

  1. http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.switch_to_frame

这篇关于在 selenium web 驱动程序中如何选择正确的 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆