硒(Python)>>selenium.common.exceptions.NoSuchFrameException: [英] Selenium (Python) >> selenium.common.exceptions.NoSuchFrameException:

查看:62
本文介绍了硒(Python)>>selenium.common.exceptions.NoSuchFrameException:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图进入iframe并在Safari的搜索栏(标记)中写文本:

I've been trying to enter to an iframe and write text in a search bar ( tag) in Safari:

我无法发布html,因为它很大而且不是我的,但这是iframe代码:

I can't post the html because it's huge and it isn't mine but this is the iframe code:

<iframe frameborder="0" id="contentIFrame0" name="contentIFrame0" title="Área de contenido" style="border: 0px none; overflow: hidden; position: absolute; left: 0px; right: 0px; height: 100%; width: 100%; visibility: visible; display: block;"> (...Content of the iframe...) </iframe>

这是python代码:

Here is python code:

wait.until(ec.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="contentIFrame0"]')))
chk_elem = wait.until(ec.visibility_of_element_located((By.XPATH, '//*[@id="2crmGrid_findCriteria"]')))
act = ActionChains(driver)
act.move_to_element(chk_elem)
act.send_keys('Search input', Keys.ENTER).perform()

但是我总是得到这个异常:"selenium.common.exceptions.NoSuchFrameException:消息:".

but I always get this Exception: "selenium.common.exceptions.NoSuchFrameException: Message:".

我一直在看一些教程,甚至在阅读官方文档,我想我的代码还可以.为什么它不起作用?

I've been watching some tutorials and even reading the official documentation and I guess my code is Ok. Why it isn't working?

PD:在我发布的iframe之前,没有其他iframe了,并且XPATHs写得很好.我不知道它是否相关,但为了防止万一,我们正在谈论Microsoft提供的动态服务网站.

PD: There aren't another iframe before the iframe that I posted and XPATHs are well written. I don't know if it is relevant but we are talking about a web site of dynamics services from microsoft, just in case.

推荐答案

此错误消息...

selenium.common.exceptions.NoSuchFrameException

...表示 Selenium 驱动 WebDriver 实例无法执行定位< iframe> 元素.

...implies that Selenium driven WebDriver instance was unable to locate the <iframe> element.

您已采用正确的方法.理想情况下,要找到的问题,您必须诱导 WebDriverWait

You have adapted the right approach. Ideally, to locate an iframe you have to induce WebDriverWait inconjunction with expected_conditions set as frame_to_be_available_and_switch_to_it().

但是,看到此错误可能有很多原因,其中一些原因和解决方案如下:

However, there can be numerous reasons behind seeing this error and some of the reasons and solutions are as follows:

  • 所需的 iframe 可能是其父 iframe 中嵌套的 iframe .在这种情况下,您必须诱使 WebDriverWait 首先切换到父iframe ,然后再切换到子iframe ,如下所示:

  • The desired iframe may be a nested iframe within it's parent iframe. In those cases you have to induce WebDriverWait first to switch to the parent iframe and then to the child iframe as follows:

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

  • 如果要在两个同级 iframe 之间切换,则首先需要先切换回 default_content ,然后按以下方式切换到同级iframe:

  • If you are switching between two sibling iframes then you first need to switch back to the default_content first and then switch to the sibling iframe as follows:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"sibling_iframe_A")))
    driver.switch_to.default_content()
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"sibling_iframe_B")))
    

  • 您可以在

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