0

I have a program that does automated data entry on the PLM platform, ENOVIA, with specific information that I input. I'm using Selenium to navigate the webpage. There is a segment where the program has to enter 4 nested frames to click a certain element. It does that well and makes the necessary edits, the problem I'm facing is when I try to exit out of the frames back to the top of the HTML code to get back to the main page using switch_to.default_content(). For some reason, after it switches into the 4th child of the nested frames and makes these edits, I see that the window changes to only consist of these frames and whats within them, and nothing above the 1st parent frame. So switch_to.default_content() doesn't seem work.

This is the code that really matters, where it switches into the 4th and final frame "DSCObjectSummary" and makes these edits


def clickCheck(Method, Locator, elemName):
    #Add If Else to check for element availibility
    #Do Retries of searchess
    try:
        wait.until(EC.element_to_be_clickable((Method, Locator)))
        print(elemName + ' Clickable')
    except NoSuchElementException:
        print("Cannot Find Element")

...... Lots of code and comes to this point
    #Switching into 4 nested frames to click "EDIT DETAILS" Button
    wait.until(EC.frame_to_be_available_and_switch_to_it("content"))
    wait.until(EC.frame_to_be_available_and_switch_to_it("detailsDisplay"))
    wait.until(EC.frame_to_be_available_and_switch_to_it("portalDisplay"))
    wait.until(EC.frame_to_be_available_and_switch_to_it("DSCObjectSummary"))
    clickCheck(By.ID, 'DSCEditObjectSummary', "Edit Details")
    elemEdit = browser.find_element_by_id("DSCEditObjectSummary")
    elemEdit.click()

    #Click the Customer Dropdown
    clickCheck(By.ID, "PMCCustomerId", "Customer Dropdown")
    elemCust = browser.find_element_by_id("PMCCustomerId")
    elemCust.click()
    elemCust.send_keys("Ces")
    """Add If Conditions to check part number"""

    #Click Document Type Field
    clickCheck(By.ID, "PMCDocumentTypeId", "Document Type")
    elemDoc = browser.find_element_by_id("PMCDocumentTypeId")
    elemDoc.click()
    elemDoc.send_keys("MOD")

    """Add Check to see if part is Active/Obsol, if Obs, make Active"""

    #Click Dashnumber field
    clickCheck(By.ID, "PMCDashNumber", "Dash Number")
    elemDash = browser.find_element_by_id("PMCDashNumber")
    elemDash.click()
    elemDash.clear()
    elemDash.send_keys("-1")

    #Click Done Button
    clickCheck(By.CSS_SELECTOR, "button[class='btn-primary'][type='button']", "Done Button")
    elemDone = browser.find_element_by_css_selector("button[class='btn-primary'][type='button']")
    elemDone.click()

    browser.switch_to.default_content()
    time.sleep(20)

I have tried switching backwards from 4th to 1st parent frame using switch_to.frame(framename) in sequence then using switch_to.default_content() only to be thrown a NoSuchFrameException. All I want is to be able to get back out of the frames, just like I got into them, without changing/removing anything in the window and get back to the top level HTML.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
A.Gomes
  • 1
  • 2

2 Answers2

0

As an alternative to driver.switch_to.default_content() you can also use:

browser.switch_to.parent_frame()

Update

As you are still facing the same issue, a better approach would be to wait for the elemDone.click() step to be completed (JavaScript / Ajax Calls) and you can induce WebDriverWait for some visibility_of_element_located() as follows:

elemDone = browser.find_element_by_css_selector("button[class='btn-primary'][type='button']")
elemDone.click()
WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "css_of_an_element_which_gets_displayed_after_the_click")))
browser.switch_to.default_content()
# as an alternative use `switch_to.parent_frame()`
# browser.switch_to.parent_frame()

Note: For debugging purpose you can replace the WebDriverWait with time.sleep(5)


Outro

“Malformed URL: can't access dead object” in selenium when trying to open google

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Unfortunately this does not fix my issue, it has the same results as default_content(). However, I have realized that the window only changes when I try to interact with the elements in the 4th nested frame using commands such as send_keys() or click(). Other than that, if I stop at only accessing the frame, I am able to exit out of them perfectly fine back into the top level using parent_frame() or default_content() and interact with other elements. Any ideas as to what could be causing this? – A.Gomes Jul 17 '19 at 18:39
  • @A.Gomes Checkout the answer update and let me know the status. – undetected Selenium Jul 17 '19 at 19:33
  • This is further helping me narrow down the problem. According to your example of **page root (grandparent) -> iframe (parent) -> iframe (child)**, in my case, when I access iframe(childs) 's elements, the page root is removed, but the parent frame, and all its children exist. I can always reload the website and restart the edit sequence, but I refuse to believe there is no effective way to exit frames back into page root. – A.Gomes Jul 17 '19 at 19:45
0

try finding all iframes and switch to the one you need.

// find all your iframes
List<WebElement> iframes = driver.findElements(By.xpath("//iframe"));
        // print your number of frames
        System.out.println(iframes.size());

        // you can reach each frame on your site
        for (WebElement iframe : iframes) {

            // switch to every frame
            driver.switchTo().frame(iframe);

            // now within the frame you can navigate like you are used to

        }
Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18
  • My problem is that the all other elements are removed as soon as I **interact** with the elements in my 4th nested frame. I can access it completely fine. How can I keep my page as it is while I edit elements within the frame? – A.Gomes Jul 17 '19 at 18:46
  • @A.Gomes are you not able to find any iframes after edit elements within the frames – Sureshmani Kalirajan Jul 17 '19 at 18:49
  • I think he is trying to switch out of the iframe to the main content. – Appu Mistri Jul 17 '19 at 18:51
  • The only iframes that exist are the 4 nested ones that I am inside, all other elements around these 4 frames do not exist. – A.Gomes Jul 17 '19 at 18:54
  • @AppuMistri Yes, but the main content disappears after interacting with the iframe, and only those exist in the window. This is what I mean by the window changes. So i cannot exit out of the frames as there's nothing to go to, I'm just confused as to why the main content disappears only to leave the frames and its contents. – A.Gomes Jul 17 '19 at 18:56
  • If there's no content there, why switch? – pcalkins Jul 17 '19 at 19:29
  • There was content before I accessed the frames, and after editing the elements within the frame, I am trying to exit back out into that content. But when I do these edits within the frame, the window changes to show only the frame and not the original content. I hope this is clear. – A.Gomes Jul 17 '19 at 19:34
  • can you post the html source ? ( once after editing the elements within the frame)? I believe we can still traverse back to root element & switch to it. Also what happen if you save the edit or simply escape it? – Sureshmani Kalirajan Jul 17 '19 at 20:11