mt246
mt246

Reputation: 13

Selenium Webdriver give NoSuchFrameException

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>

Upvotes: 1

Views: 5929

Answers (1)

alecxe
alecxe

Reputation: 473833

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()

Upvotes: 1

Related Questions