Selenium-从frame [0]切换到window [1]后出现NoSuchElementException-Firefox GeckoDriver错误-Python [英] Selenium - NoSuchElementException after switching from frame[0] to window[1] - Firefox GeckoDriver Error - Python
问题描述
我在Firefox GeckoDriver浏览器中收到错误element not found
I have receive an error, element not found
in the Firefox GeckoDriver browser
当我从窗口[1]切换到帧[0],回到窗口[1],然后单击关闭帧按钮时.
when i switch from window[1] to frame[0], back to window[1], and then click the close frame button.
我想继续使用GeckoDriver
,因为我遇到了ChromeDriver
的性能问题.抱歉,这是一个内部网站,我不能分享HTML
.
I want to continue using GeckoDriver
because i was having performance issues with ChromeDriver
. Sorry this is an internal website and I can't share the HTML
.
- 我已完成以下操作以查明问题所在.
- 使用完全相同的代码进行测试,并且可以在Chrome驱动程序中正常工作
- 当我切换到window [1]时,在Firefox中工作,单击
openframe
按钮,然后单击closeframe
按钮,而不切换到frame [0]. - 我可以切换到帧[0],切换回window [1]并执行
driver.close()
,它将关闭window [1]. - 从帧[0]切换回后,我可以从window [1]读取
driver.page_source
,并且看到该元素仍在page_source
中并且没有更改. - 我可以从帧[0]切换回窗口[0],然后单击按钮重新打开窗口[1],这样可以正常工作.然后,我可以单击打开和关闭框架按钮.
- I have done the following to pinpoint the issue.
- Tested with exact same code and works properly in Chrome driver
- Works in Firefox when i switch to window[1] click the
openframe
button, and then click thecloseframe
button, without switching to frame[0]. - I can switch to frame[0], switch back to window[1] and do a
driver.close()
and it will close window[1]. - I can read the
driver.page_source
from window[1] after switching back from frame[0] and see that the element is still in thepage_source
and has not changed. - I can switch from frame[0] back to window[0] and click the button to re-open window[1] and that works properly. Then i can click the open and close frame buttons.
因此问题仅在于从frame [0]切换回window [1],然后尝试在Firefox GeckoDriver中单击window [1]中的元素.
So the problem just resides on switching from frame[0] back to window[1], then trying to click an element in window[1] in Firefox GeckoDriver.
Python:2.7
硒:3.0.1
GeckoDriver:v0.13.0
Firefox:51.0.1
Python: 2.7
Selenium: 3.0.1
GeckoDriver: v0.13.0
Firefox: 51.0.1
这是代码:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver.get('www.internalwebsite.com')
driver.find_element_by_id('opensnewwindow').click()
driver.switch_to_window(driver.window_handles[1])
driver.find_element_by_id('opennewframe').click()
driver.switch_to_frame(0)
//read contents
driver.switch_to_window(driver.window_handles[1])
driver.find_element_by_id('closeframe').click()
错误:
selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:[id ="closeframe"]
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="closeframe"]
推荐答案
下面的代码似乎可以解决问题.仍然不确定为什么.
The code below seemed to do the trick. Still not sure why though.
driver.switch_to.default_content()
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver.get('www.internalwebsite.com')
driver.find_element_by_id('opensnewwindow').click()
driver.switch_to_window(driver.window_handles[1])
driver.find_element_by_id('opennewframe').click()
driver.switch_to_frame(0)
//read contents
driver.switch_to.default_content()
driver.switch_to_window(driver.window_handles[1])
driver.find_element_by_id('closeframe').click()
这篇关于Selenium-从frame [0]切换到window [1]后出现NoSuchElementException-Firefox GeckoDriver错误-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!