需要帮助尝试切换框架

时间:2019-07-30 14:53:13

标签: python selenium

在我的程序中循环时,我试图切换到的帧在第一个循环后发生变化,然后我无法访问它,而第二个循环失败。帧名称每次都会更改3倍,这是我最初切换帧的示例:


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"))

如何在每个帧中再添加3个,以便在第一个循环之后在正确的帧中结束?

这是元素(我更改了SRC的时间很长):


<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()

1 个答案:

答案 0 :(得分:2)

如果您打算循环遍历所有iframes,我将首先使用一个更通用的选择器来获取所有这些元素,然后在每个循环中使用for

# 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)

一种潜在的方法,当最初无法如评论中所述获得它们时,您可以跟踪增量(始终为3)和乘数(每次增加1)。 :

# 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

以下是乘数每次增加1时发生的情况的数学公式:

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...