Reputation: 155
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
Upvotes: 6
Views: 7401
Reputation: 2148
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)
Upvotes: 1
Reputation: 5240
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)
Upvotes: 4