硒(Python)>>selenium.common.exceptions.NoSuchFrameException: [英] Selenium (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.
您已采用正确的方法.理想情况下,要找到 iframe 的问题,您必须诱导 WebDriverWait 与 frame_to_be_available_and_switch_to_it().
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")))
-
有时,多个 iframe 对于相同的属性可以具有相似的值.在这种情况下,您可能需要构建定位器策略使用
src
属性唯一地标识< iframe>
元素,如下所示:At times multiple iframes can have similar values for same attributes. In those cases, you may need to construct Locator Strategies which identifies the
<iframe>
element uniquely using thesrc
attribute as follows:WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id="iframe_id and @src='iframe_src'"]")))
-
最后,您需要确保二进制版本兼容,并且可以在以下位置找到几个相关的讨论:
Finally, you need to ensure that the binary versions are compatible and you can find a couple of relevant discussions in:
为 WebDriverWait 配置的超时可能太少了,在这种情况下,您需要按以下方式增加超时时间:
The timeout configured for WebDriverWait may be too less and in that case you need to increase the timeout as follows;:
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"parent_iframe_xpath")))
您可以在以下位置找到一些参考讨论:
You can find a couple of reference discussions in: