Skip to content Skip to sidebar Skip to footer

Multiple Iframe Tags Selenium Webdriver

I am trying to use selenium to send credit card info onto a site, and each element is contained in a separate iframe tag in the HTML. For example, the box to enter a credit card nu

Solution 1:

You have correctly found the first iframe tag which contains the credit card number field to enter the card number and sent the appropriate keys. Moving forward when you want to sent the appropriate keys to the card holder name field you need to be on the relevant frame first. So you have to switch back to the immedediate parent_frame() which contains both the iframe tags i.e. first iframe and second iframe and next try to switch to the second iframe to locate the card holder name field as follows :

# as there are multiple frames so find_element_by_tag_name("iframe") will not suffice and have to use unique css/xpath
driver.switch_to.frame(driver.find_element_by_xpath("xpath_of_iframe1"))
cc = driver.find_element_by_id("number")
cc.send_keys(credit_card_number)
driver.switch_to.default_content() #incase iframe1 and iframe2 are immediate child of the Top Level Content
driver.switch_to.frame(driver.find_element_by_xpath("xpath_of_iframe2"))
cc = driver.find_element_by_id("name")
cc.send_keys(cc_name)

Note : As per best practices you should not detect frames through find_element_by_tag_name("iframe") inpresence of multiple <iframe> tags on the webpage. The best way would be to use either Frame Name , Frame ID or Frame Index. You can find the detailed discussion in How can I select a html element no matter what frame it is in in selenium?.

Solution 2:

After switching to first iframe , you have to switch back to default context (your main page containing multiple iframes) & then switch to next iframe .

So your pseudo code should look like

driver.switch_to.frame(driver.find_element_by_tag_name("iframe_1"))
driver.switch_to.defaultContent()
driver.switch_to.frame(driver.find_element_by_tag_name("iframe_2"))
driver.switch_to.defaultContent()
& so on

Post a Comment for "Multiple Iframe Tags Selenium Webdriver"