0

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"))
3
  • Does the frame have an ID? I'm pretty sure you can lookup the frame by ID Commented Aug 8, 2017 at 15:02
  • Possible duplicate of selecting an iframe using python selenium Commented Aug 8, 2017 at 17:01
  • Can you share the relevant HTML or the URL please? Commented Aug 9, 2017 at 5:02

2 Answers 2

2

You can actually select an iFrame using the below methods: -

  • frame(index)
  • frame(Name of Frame [or] Id of the frame)
  • frame(WebElement frameElement)
  • defaultContent()

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 :-

enter image description here

Hope it will help you :)

Sign up to request clarification or add additional context in comments.

Comments

0

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 .

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.