6

So I'm trying to select a frame using selenium. I found many examples and similar questions on stackoverflow regarding this.

All of them pointed out that to select elements that are in a frame you first have to "move" to that frame and then get the element you want. But I can't seem to get the frame that I want.

All the post I read said use:

driver.switch_to.frame("contentfrm") # Fetch by name, or
driver.switch_to.frame(1) # Fetch by index

So I tries all of them and no matter what I use I always get an error that says it cannot find the frame, selenium.common.exceptions.NoSuchFrameException: Message: Unable to locate frame: contentfrm

The python code:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get("http://10.0.0.2/html/content1.asp")
driver.get("10.0.0.2")

elem = driver.find_element_by_name("Username")
elem.send_keys("admin")

elem = driver.find_element_by_name("Password")
elem.send_keys("admin2")

elem.send_keys(Keys.ENTER)

# ----
driver.switch_to.default_content()
driver.switch_to.frame("contentfrm")
# elem = driver.find_element_by_id("m8")

# driver.quit()

The HTML code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Quick Start</title>
</head>

<frameset onload="load()" rows="0,*" frameborder="0" border="0" framespacing="0">
    <frame name="topFrame" scrolling="no" src="../main/refresh.asp" __idm_frm__="200"></frame>
    <frame name="contentfrm" id="contentfrm" __idm_frm__="201"></frame>
</frameset>

</html>

Note: The code works correctly up to where I have to select the frame.

Any idea why this is happening

3
  • that frame has no content are you sure you don't want the topFrame? Commented May 31, 2016 at 13:23
  • @Rafael It actually does have content. I just didn't include it otherwise there would be to much code. But the element that I am looking to get is located in that frame Commented May 31, 2016 at 13:31
  • try removing default content before switch statement and also give some wait time before finding the element ``` driver.switch_to.frame("contentfrm"); WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(" "))); ``` Commented May 31, 2016 at 14:01

2 Answers 2

4

Without the website in question it's hard to pinpoint what the problem is but I will take a few assumptions:

  • Your frameset is doing some javascript on the function load() which may make the elements unavailable when you try to find them.

  • The frame element you're getting has no src attribute so it will have no content

  • Maybe the selection in the function switchto.frame() doesn't work the way you think it does.

To be safe it's better to wait for the element to be visible and then select it by xpath

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//frame[@name='contentfrm']"))
)
driver.switch_to.frame(element)
Sign up to request clarification or add additional context in comments.

Comments

1

As @Rafael said, it is better to use a different locator, and you should check what the on load() javascript is doing. However I would suggest using the id of the frame as the locator and not an xpath for the frame name.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.