Ricard Le
Ricard Le

Reputation: 55

how to locate an element embedded in iframe for Python Selenium

I am having problem locating a single report element embedded in iframe. There many reports contains in table where iframe is located. Also using the firefox firePath it is given me a very link and am not sure how to remove the long strings. Edited the code:

    browser.switch_to_frame(browser.find_element_by_tag_name("iframe"))
    # set the context on the child frame
    browser.switch_to_frame(browser.find_element_by_id("__gwt_historyFrame"))

    # set the context on the next child frame
   wait = WebDriverWait(self.browser, 10)
        browser.switch_to_frame(wait.until(EC.presence_of_element_located(By.ID,"com.demandreports.wb.Workbench")))

    #click the link
    wait.until(EC.presence_of_element_located(By.LINK_TEXT,"Invoice Aging Report")).click()

    # switch back to the main document context
    browser.switch_to_default_content()

This is error:

    Traceback (most recent call last):
  File "C:\Python34\zuoraDataexport6.py", line 118, in <module>
    scraper.scrape()
  File "C:\Python34\zuoraDataexport6.py", line 57, in scrape
    browser.switch_to_frame(browser.find_element_by_id("com.demandreports.wb.Workbench"))
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 234, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"com.demandreports.wb.Workbench"}

This is what i want to locate: Invoice Aging Report

This is the html:

<document>
    <html style="overflow: hidden;">
    <head>
    <body style="margin: 0px;">
    <iframe id="__gwt_historyFrame" style="position:absolute;width:0;height:0;border:0" tabindex="-1" src="javascript:''"/>
    <iframe id="com.demandreports.wb.Workbench" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"/>
    <table class="Workbench-Page" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
    <tr>
    <tr>
    </tbody>
    </table>
    </body>
    </html>
</document>

Upvotes: 1

Views: 3147

Answers (1)

Florent B.
Florent B.

Reputation: 42518

The context is not set on the frame where your element is. You need to switch to each frame on the tree line:

# set the context on the child frame
browser.switch_to_frame(browser.find_element_by_id("__gwt_historyFrame"))

# set the context on the next child frame
browser.switch_to_frame(browser.find_element_by_id("com.demandreports.wb.Workbench"))

#click the link
wait.until(EC.presence_of_element_located(By.LINK_TEXT,"Invoice Aging Report")).click()

# switch back to the main document context
browser.switch_to_default_content()

Upvotes: 1

Related Questions