无论 html 元素在 selenium 中的哪个框架,我如何选择它? [英] How can I select a html element no matter what frame it is in in selenium?

查看:21
本文介绍了无论 html 元素在 selenium 中的哪个框架,我如何选择它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择驻留在 iframe 内和可能驻留在其他 iframe 中的元素.

是否有可能在(python)selenium 的某个(子)iframe 中选择一个元素而不选择之前的 iframe?有没有办法以某种方式循环"每个 iframe 并检查在哪里可以找到我的元素......?

如果元素和 html 内容以及 iframe 可能即将被加载,如何做到这一点......?

解决方案

,无法与其中的任何 WebElement 进行交互iframeSelenium 无需切换到相应的 iframe.

原因:

加载页面时,Selenium 的焦点默认保持在 Top Window.Top Window 包含其他 iframesframesets强>.因此,当我们需要与 iframe 中的 WebElement 进行交互时,我们必须通过下面提到的方法之一切换到相应的 iframe :

帧切换方法:

我们可以通过 3 种方式切换到框架.

按框架名称:

Name iframe 的属性,通过它我们可以切换到它.

示例:

driver.switch_to.frame("iframe_name")

按帧 ID:

ID iframe 的属性,通过它我们可以切换到它.

示例:

driver.switch_to.frame("iframe_id")

按帧索引:

假设页面有10个frame,我们可以通过index切换到iframe.

示例:

driver.switch_to.frame(0)driver.switch_to.frame(1)

切换回主框架:

我们可以使用default_content()parent_frame() 切换回主框架>

示例:

driver.switch_to.default_content()driver.switch_to.parent_frame()

<小时>

切换帧的更好方法:

切换帧的更好方法是使用 expected_conditions 诱导 WebDriverWait 以获得预期帧的可用性> 设置为 frame_to_be_available_and_switch_to_it 如下:

  • 通过Frame ID:

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

  • 通过帧名称:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"name_of_iframe"))

  • 通过Frame Xpath:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"xpath_of_iframe"))

  • 通过Frame CSS:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"css_of_iframe"))

<小时>

参考

您可以在以下位置找到相关的详细讨论:

I am trying to select an element which resides inside an iframe and probably in other iframes.

Is it somehow possible to select an element within some (sub-)iframe in (python)selenium without selecting the iframes before? Is there a way to 'loop' over every iframe somehow and to check where to find my element...?

And how to do that in the case elements and html stuff and iframes might just about being loaded...?

解决方案

No, it is not possible to interact with any WebElement within an iframe through Selenium without switching to the respective iframe.

Reason :

When a page is loaded, Selenium's focus by default remains on the Top Window. The Top Window contains the other iframes and the framesets. So when we need to interact with a WebElement which is within an iframe we have to switch to the respective iframe through one of the below-mentioned methods :

Frame Switching Methods :

We can switch over to frames by 3 ways.

By Frame Name :

Name attribute of iframe through which we can switch to it.

Example:

driver.switch_to.frame("iframe_name")

By Frame ID :

ID attribute of iframe through which we can switch to it.

Example:

driver.switch_to.frame("iframe_id")

By Frame Index :

Suppose if there are 10 frames in the page, we can switch to the iframe by using the index.

Example:

driver.switch_to.frame(0)
driver.switch_to.frame(1)

Switching back to the Main Frame :

We can switch back to the main frame by using default_content() or parent_frame()

Example:

driver.switch_to.default_content()
driver.switch_to.parent_frame()


A Better Approach to Switch Frames:

A better way to switch frames will be to induce WebDriverWait for the availability of the intended frame with expected_conditions set to frame_to_be_available_and_switch_to_it as follows :

  • Through Frame ID :

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

  • Through Frame Name :

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"name_of_iframe"))
    

  • Through Frame Xpath :

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"xpath_of_iframe"))
    

  • Through Frame CSS :

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"css_of_iframe"))
    


Reference

You can find a relevant detailed discussion in:

这篇关于无论 html 元素在 selenium 中的哪个框架,我如何选择它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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