Reputation: 61
I am trying to select a frame using Selenium in Python. The webpage contains a number of frames which are nested under a frameset element. I tried selecting a frame with the name "fraMenu" using:
driver.switch_to.frame(driver.find_element_by_name("fraMenu"))
but I am getting the NoSuchElementException with the error message: Message: Unable to locate element: [name="fraMenu"]
The complete code is below:
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("someurl")
# switch back to default frame
driver.switch_to.default_content()
# switch to frame with name fraMenu
driver.switch_to.frame(driver.find_element_by_name("fraMenu"))
Upvotes: 0
Views: 3487
Reputation: 17593
You can actually select an iFrame using the below methods: -
So you can switch by passing the any above information about the frame. Yes you need to switch everytime according to require action
Example:-
driver.SwitchTo().Frame("top");
.... Perform your action on frame
driver.SwitchTo().defaultContent();
driver.SwitchTo().Frame("navigation");
.... Perform your action on frame
driver.SwitchTo().defaultContent();
....
Now you have to find the hierarchy of your nested frame and switch all one by one.
use chrome dev tools and select the element you can see the hierarchy .. just switch from parent to child till your element not reach. perform your operation and switch back to default
Example :-
Hope it will help you :)
Upvotes: 2
Reputation: 41
Did you try to wait some time before trying to find it? Maybe the frame didn't have time to load. I'd suggest you start with time.sleep(4)
and if it works, then try to wait until the frame is loaded with an expected condition .
Upvotes: 0