问题
I have receive an error, element not found
in the Firefox GeckoDriver browser
when i switch from window[1] to frame[0], back to window[1], and then click the close frame button.
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
.
- 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.
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
Selenium: 3.0.1
GeckoDriver: v0.13.0
Firefox: 51.0.1
Here is the code:
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()
Error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="closeframe"]
回答1:
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()
来源:https://stackoverflow.com/questions/42033485/selenium-nosuchelementexception-after-switching-from-frame0-to-window1-f