Selenium is not just a singular tool or an API but consists of many tools such as:
During the automation process, the user has to select different elements using the selection methods provided by Selenium. Some of these elements are enclosed in an iframe
tag. Therefore, these elements cannot be selected directly. First, the user has to select this frame and then navigate to the element.
The following example shows a button tag enclosed in an iframe
tag.
<div id="modal"><iframe id="buttonframe" name="myframe"<button>Click here</button></iframe></div>
div
tag with id="modal"
.iframe
tag with id="buttonframe"
and name="myframe"
.button
tag with the text "Click here."iframe
tag.div
tag.If the button tag is not in the iframe
, we will click on the button
using the following line of python code.
driver.find_element(By.TAG_NAME, 'button').click()
button
.However, we may see a no element error
if the page has no buttons outside the iframe
. This happens because selenium can only access the elements in the top-level document.
iframe
tagWe must follow the following steps to work with the button
inside an iframe
.
iframe
.iframe
.iframe
. iframe
tag and access a button
The web controller offers the following three ways to switch to a frame.
Let us see an example of each case of accessing the iframe
tag and clicking on the button
.
Switching using a web element is the most flexible option. We can find the frame using our preferred selector and switch to it.
iframe = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")driver.switch_to.frame(iframe)driver.find_element(By.TAG_NAME, 'button').click()
iframe
tag using CSS selector and assign it to the variable named iframe
.iframe
tag.button
tag inside that iframe
.We can use the ID or name attribute if our framework has the appropriate attribute. If we have multiple elements on the page with the same ID or name, the first one found is selected.
driver.switch_to.frame('buttonframe')driver.find_element(By.TAG_NAME, 'button').click()
button
tag inside that iframe
.if the page does not have a unique id or tag, then we can use the index to select a specific iframe
or other tags from the page
iframe = driver.find_elements_by_tag_name('iframe')[1]driver.switch_to.frame(iframe)driver.find_element(By.TAG_NAME, 'button').click()
Note: During selection, when we use an
element
in the code, it returns only the first element, while if we useelements
, it will return the list of all elements with the same tag, ID, or name. If there is only one element on the page and we useelements
, it will return the list with one element. If the page does not have an element with the selected tag,elements
will not return theno element error
. Instead, it will return an empty list.
elements
to get the list of elements with the tag name iframe
. Then select the index number 1
and save the selected element in a variable named iframe
.iframe
.button
tag inside that iframe
.When we are done with the element inside an iframe
, we have to move out of that iframe
to access the other content of the page which is not in that iframe
.
driver.switch_to.default_content()
iframe
and switch back to the page's default content.Free Resources