Loons22
Loons22

Reputation: 373

Need help trying to switch frames

The frame I'm trying to switch to when looping back through my program changes after the first loop and then I'm not able to access it and the second loop fails. The frame name changes by a factor of 3 every time, here is an example of what I did to switch frames originally:


driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-1']/iframe"))

Next frame

driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-4']/iframe"))

Next frame

driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-7']/iframe"))

How would I add another 3 to each frame so I end up in the right frame after the first loop?

Here is the element(I changed the SRC because of how long it was):


<iframe width="100%" height="100%" scrolling="auto" frameborder="0" src="/website/impactingActivities.do.dialogId=ui-id-1" onload="top.hideLoadingDiv('ui-id-1','ui-id-1');top.rightclickdisable('ui-id-1')" style="visibility: visible;"></iframe>


def AccountSearch():

    def get_iframe_id(multiplier):
        delta = 3
        return str(1 + delta * multiplier)

    mult = 0

    start = time.time()

    driver.switch_to.default_content()

    driver.switch_to.frame("ifrmLeftNavSearch")

    searchbar = driver.find_element_by_id("searchParam")

    MyAccountNumber = (AccountEntry.get())

    searchbar.send_keys(MyAccountNumber)

    searchbar.send_keys(Keys.ENTER)

    newsearchbar = driver.find_element_by_id("searchParam")

    newsearchbar.send_keys(Keys.DELETE)

    driver.switch_to.frame("ifrmSearchResults")


    searchresults = driver.find_element_by_class_name("bodytextlink").click()


    print("***************************** Impacting Balance ********************************")


    driver.switch_to.default_content()
    driver.switch_to.frame("ifrmContent")

    action = ActionChains(driver)
    Activity = action.move_to_element(driver.find_element_by_xpath("//*[@id='divMenu23']")).perform()
    time.sleep(0.5)
    ActImpct = action.move_to_element(driver.find_element_by_xpath("//*[@id='divMenu266']")).perform()
    time.sleep(1)
    ClickImpct = driver.find_element_by_xpath("//*[@id='divMenu266']").click()

    driver.switch_to.default_content()

    #start of for or where loop

    time.sleep(5)
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))


    try:
        wait = WebDriverWait(driver, 0.5)
        Row3 = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='tblDetails']/tbody/tr[3]")))
        print("ACTIVITY IMPACTING BALANCE, REVIEW NEEDED")
        print("\n")
    except:
        print("No Activity Impacting Balance")
        print("\n")


    driver.find_element_by_xpath("//*[@id='tblButtonBar2']/input").click()

        #Return to Summary

    driver.switch_to.default_content()
    driver.switch_to.frame("ifrmContent")
    driver.find_element_by_xpath("//*[@id='divMenu4']").click()


    print("***************************** Port Management *****************************")

    print("\n")

    driver.switch_to.default_content()
    driver.switch_to.frame("ifrmContent")
    driver.switch_to.frame("ifrmPage")

    PortManagement = driver.find_element_by_xpath("//*[@id='tblData']/tbody/tr[1]/th[4]/a").click()


    driver.switch_to.default_content()

    mult += 1
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))


    try:
        CashEquiv = driver.find_element_by_xpath("//tr//*[contains(text(), 'Equivalent')]//following::td[4]")
        print("Cash Equivalent:", CashEquiv.text)
        print("\n")
    except:
        print("No Cash Equivalents")
        print("\n")

    try:
        USdollar = driver.find_element_by_xpath(("//tr//*[contains(text(), 'US Dollar')]//following::td[4]")).text
        print("US Dollar Amount:", USdollar.replace("\n", ' '))
        print("\n")
    except:
        print("Error, review US Dollar manually")
        print("\n")

    driver.find_element_by_xpath("//*[@id='tblDialogButtonBar']/tbody/tr/td[2]/input").click()


    print("***************************** Transaction Inquiry ******************************")

    print("\n")

    driver.switch_to.default_content()
    driver.switch_to.frame("ifrmContent")

    action = ActionChains(driver)
    Activity = action.move_to_element(driver.find_element_by_xpath("//*[@id='divMenu23']")).perform()
    TrnsInq = action.move_to_element(driver.find_element_by_xpath("//*[@id='divMenu40']")).perform()
    ClickTrnsInq = driver.find_element_by_xpath("//*[@id='divMenu40']").click()

    driver.switch_to.default_content()

    mult += 1
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))


    try:
        ClosingFee = driver.find_element_by_xpath("//tr//*[contains(text(), 'Closing')]//following::td[5]")

        print("Closing Fee:", ClosingFee.text)

        print("\n")

    except:
        print("Closing Fee: ** No Closing Fee Charged **")
        print("\n")


    CloseTransactionInquiry = driver.find_element_by_xpath("//*[@id='tblDialogButtonBar']/tbody/tr[2]/td[3]/input").click()

    end = time.time()

    totaltime = end - start
    print("Time Elapsed:", totaltime)


SearchButton = Button(window, text="Search", command=AccountSearch)
SearchButton.grid(column=1, row=8)
SearchButton.configure(background = "light grey")

window.mainloop()

Upvotes: 0

Views: 112

Answers (1)

mrfreester
mrfreester

Reputation: 1991

If you're planning on looping through all of the iframes I would grab all of those elements first with a more general selector, and then use a for each loop.

# grab all of your iframes at once if you have access to them
iframeElements = driver.find_elements_by_xpath("//*[contains(@id, 'ui-id']/iframe")

for iframe in iframeElements:
    driver.switch_to.frame(iframe)

A potential approach for when they are not all obtainable at first as mentioned in the comments, you could keep track of the delta, which is always 3, and the multiplier, which increases by 1 each time. :

# outside of loop
def get_iframe_id(multiplier):
    delta = 3
    return str(1 + delta * multiplier)

mult = 0

#start of for or where loop
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))

    # Next frame
    mult += 1
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))

    # Next frame
    mult +=1
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id = 'ui-id-" + get_iframe_id(mult) + "']/iframe"))

    mult += 1

    # Whatever you do to check for breaking out of loop
# End of loop

Here is the math spelled out for what's going on with the multiplier increasing by 1 each time:

id = 1 + delta * multiplier
1 = 1 + 3 * 0
4 = 1 + 3 * 1
7 = 1 + 3 * 2
10 = 1 + 3 * 3
13 = 1 + 3 * 4
16 = 1 + 3 * 5
etc...

Upvotes: 2

Related Questions