Exit an Iframe Selenium Pyhton

Asked

Viewed 335 times

0

I’m having a hard time... I have a page to manipulate with Selenium, my problem is Iframes. I access a main Iframe and within that main iframe I have other iframes to manipulate, my porblema is that I can’t get out of the first Iframe that access, to be able to manipulate another Iframes and the main Iframe.

    #frame principal
    Iframe = self.Web_Browser.find_element_by_xpath(
        '//*[@id="ModalCommand_modal"]/div/div/div[2]/iframe')
    self.Web_Browser.switch_to.frame(Iframe)
    try:
        # button folder
        self.Web_Browser.find_element_by_xpath(
            '//*[@id="Button"]/div/div[1]/div/span/div/span[2]/a').click()
        sleep(1)
    except Exception:
        self.Web_Browser.find_element_by_xpath(
            '//*[@id="Button-M"]/div/div[1]/div/span/div/span[2]/a').click()
    #iframe Folder
    def Frame():
        Iframe_folder = self.Web_Browser.find_element_by_xpath('//*[@id="Iframe_folder"]/div/div/div[2]/iframe')
        self.Web_Browser.switch_to.frame(Iframe_folder)
        self.Web_Browser.find_element_by_id('Resultado_SearchField').send_keys('dados')
        self.Web_Browser.find_element_by_id('Resultado_SearchButton').click()
        sleep(2)
        click = self.Web_Browser.find_element_by_xpath(
            '//*[@id="Resultado_Simpl"]/tbody/tr[1]/td[1]/a')
        print(click.is_displayed())
        try:
            ActionChains(self.Web_Browser).move_to_element(click).click(click).perform()
        except Exception as e:
            print('\tError: Nenhuma Pasta encontrada')
        self.Web_Browser.switch_to.default_content()
        sleep(1)
    Frame()

    self.Web_Browser.find_element_by_xpath('//*[@id="Button"]/div/div[2]/div/span/div/span[2]/a').click()
    print(elemento.is_displayed())

I get this mistake:

Selenium.common.exceptions.Nosuchelementexception: Message: Unable to locate element: //*[@id="Button"]/div/div[2]/div/span/div/span[2]/a

I have no idea why I can’t get out of this Iframe.

1 answer

1


After doing what you needed in iframe, you should return to the starting document.

Would look like this:

self.Web_Browser.switch_to.default_content() # Linha adicionada antes da ação do click.
self.Web_Browser.find_element_by_xpath('//*[@id="Button"]/div/div[2]/div/span/div/span[2]/a').click()

But it turns out you don’t want to go back to the original document, you want to go back to the main iframe, because with switch_to.default_content() you would go to the initial document, and that button is not there.

Return to the iframe you want, which is already declared up there:

self.Web_Browser.switch_to.frame(Iframe)
self.Web_Browser.find_element_by_xpath('//*[@id="Button"]/div/div[2]/div/span/div/span[2]/a').click()

And if you cannot return directly, you should go to the initial document and then go to the main iframe:

self.Web_Browser.switch_to.default_content()
self.Web_Browser.switch_to.frame(Iframe)
self.Web_Browser.find_element_by_xpath('//*[@id="Button"]/div/div[2]/div/span/div/span[2]/a').click()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.