1
votes

I want to select the frame in the web page and it failed to select frame in Selenium IDE. And when I use the Webdriver, it comes an NoSuchFrameException:

driver.find_element_by_name("GO").click()
driver.find_element_by_id("EO_PE_SRCH_INP_EO_PE_URLTEXT$5").click()
driver.switch_to.frame("contentFrame")

driver.switch_to.frame("contentFrame") File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/switch_to.py", line 64, in frame self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference}) File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 173, in execute self.error_handler.check_response(response) File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 166, in check_response raise exception_class(message, screen, stacktrace) NoSuchFrameException: Message: Unable to locate frame: contentFrame Stacktrace: at FirefoxDriver.prototype.switchToFrame (file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/[email protected]/components/driver-component.js:9710:11) at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/[email protected]/components/command-processor.js:11635:16) at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/[email protected]/components/command-processor.js:548:5)

HTML code:

<head></head>
<frameset id="fm2" frameborder="0" cols="150,*" framespacing="0" border="0">
    <frame src="https://xxxxxxxxxxx/servlet/Main/menu" scrolling="yes" name="mainMenuFrame"></frame>
    <frame noresize="" name="contentFrame" src="https://xxxxx/servlet/Main/main?usertype=2">
        #document
    </frame>
</frameset>

1
Are you trying to switch to contentFrame from top window or from another frame? If its another frame switch to default content then perform switch to contentFrame...Vivek Singh
Actually, when i clicked "EO_PE_SRCH_INP_EO_PE_URLTEXT$5", a new window will pop up and i want to select the contentFrame in the new windowmt246
If clicking "EO_PE_SRCH_INP_EO_PE_URLTEXT$5" opens a new window den u need to switch to that window first and then switch to frame. U r directly switching to frame without switching to the window. That will definetly give you error cause contentFrame is not available in main window, its in the child window that popped up...Vivek Singh

1 Answers

1
votes

This is a frameset you are dealing with, make it in two steps:

frame = driver.find_element_by_id("fm2")
driver.switch_to.frame(frame)

driver.switch_to.frame("contentFrame")

You can also wait for the frame explicitly:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

frame = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "fm2")))
driver.switch_to.frame(frame)

driver.switch_to.frame("contentFrame")

UPDATE (using a real-world example provided in comments):

It appears that you need to switch to the frame by index:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.ihp.hku.hk/sfb.html')

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "frameset")))
driver.switch_to.frame(1)

driver.find_element_by_link_text('Badminton: max 4 persons per court').click()