【问题标题】:looping through a set of iframes循环通过一组 iframe
【发布时间】:2020-05-21 16:25:03
【问题描述】:

程序如何运作? 使用循环下载多个文件,在您选择它在带有下载图标的 iframe 中打开的文档后,按下文件下载后,程序关闭 iframe 并重新启动循环,选择一个新项目等等,问题在打开 iframe 并选择它后发生在第二个循环中,它无法找到下载图标。 它在同一个 iframe 中具有相同 xpath 的相同图标如何在第二次尝试中找不到?

def DownloadCurrentApprovals():
table=driver.find_element_by_xpath('//*[@id="ctl00_MainContent_ApprovalGridViewControl_ApprovalGridViewInfo_ctl00"]')
i = 0
for tr in table.find_elements_by_tag_name("tr"):
    #selecting the item to be printed 
    robo = driver.find_element_by_xpath('//*[@id="ctl00_MainContent_ApprovalGridViewControl_ApprovalGridViewInfo_ctl00__{}"]'.format(i)).click()
    #press the download icon which will open the iframe
    robo = driver.find_element_by_xpath('//*[@id="ctl00_MainContent_ApprovalGridViewControl_ApprovalGridViewInfo_ctl00_ctl02_ctl00_ApprovalRadToolBar"]/div/div/div/ul/li[2]/a/span/span/span/span').click()
    driver.implicitly_wait(10)
    #get the frame
    frame = driver.find_element_by_xpath('//*[@id="RadWindowWrapper_ctl00_MainContent_RadWindowApprovalReport"]/table/tbody/tr[2]/td[2]/iframe')
    driver.switch_to.frame(frame)
    #pressing the download icon inside the iframe
    element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ReportViewer_ReportToolbar_PrintGr_Print_Button"]/tbody/tr/td/input[1]')))
    #switching to close the iframe
    driver.switch_to.default_content()
    robo = driver.find_element_by_xpath('//*[@id="RadWindowWrapper_ctl00_MainContent_RadWindowApprovalReport"]/table/tbody/tr[1]/td[2]/table/tbody/tr/td[3]/ul/li[5]/a').click()
    i+=1

错误 --raise TimeoutException(消息、屏幕、堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:

table and rows code

【问题讨论】:

    标签: python selenium iframe


    【解决方案1】:

    如果您可以分享更多信息,那会有所帮助,但我仍然会尝试推断问题。我觉得你想尝试的是去表格的每一行选择文档然后切换到 iframe 并下载它。我在这里建议两个选项,您可以选择其中任何一个,如果没有帮助,请分享 dom 结构的快照,以便我更好地理解它。

    选项 1:我看到,为了遍历表的每一行,您正在为表中的每一行运行一个循环,并且在下面的语句中,您使用了动态 xpath robo = driver.find_element_by_xpath('//*[@id="ctl00_MainContent_ApprovalGridViewControl_ApprovalGridViewInfo_ctl00__{}"]'.format(i)).click() 这看起来不错,但是在以下语句中,您使用了静态 XPath,它会一次又一次地指向同一个元素, 例如这个语句总是指向表的第二行 frame = driver.find_element_by_xpath('//*[@id="RadWindowWrapper_ctl00_MainContent_RadWindowApprovalReport"]/table/tbody/tr[2]/td[2]/iframe') 我不确定 dom 的外观如何,但如果您在遍历表的行时使用动态 XPath 来保持指向新行,它会更好。

    选项 2 如果您正在迭代该行,您可以使用该行对象来查找该行中的子元素,如下所示 tr.find_element_by_id() or tr.find_element_by_tag_name() 等(最好不要使用 xpath,因为 xpath 不会查找子元素,而是从 dom 的开头开始搜索,在这种方法中您也可以使用 xpath,但它应该是动态的。) 如果此修复解决了您的问题,请告诉我,如果您有任何问题,请发表评论,也请在快照中分享表格的 dom 结构,我将能够更好地帮助您。

    【讨论】:

    • 谢谢你的帮助,我想说我的问题不在循环中,循环工作并选择表格中的第二项并打开 iframe 下载,它找不到图标在 iframe 里面这一行` element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ReportViewer_ReportToolbar_PrintGr_Print_Button"]/tbody/tr/td/input[1]')))`
    • 循环将正常工作,直到下面的语句,因为您使用动态 XPath 来识别与行对应的元素,但后来您使用的 xpath 是静态 driver.find_element_by_xpath('//*[@ id="ctl00_MainContent_ApprovalGridViewControl_ApprovalGridViewInfo_ctl00__{}"]'.format(i)).click() 您可以使用动态 XPath 指向下一次迭代中的下一行,如果您可以在此处共享表结构,我可以建议更好。
    • 我认为如果 iframe xpath 发生变化并且不一样,我会得到一个错误,它不存在但我不明白,相反,程序找到 iframe 但它找不到里面的项目它。对不起,我有点菜鸟
    • 这是因为您单击了新行(第 2 行)中存在的文档以进行新迭代(因为您使用了动态 XPath,并且它在每次迭代中都会发生变化),它标识的 iframe 属于到之前的迭代,该迭代仍然存在于 dom 中,但不显示具有该 ifrme 的内容(因为这里您使用静态 XPath,它仍然指向作为第 1 行的一部分的相同 iframe)所以当您切换到旧 iframe 时内容未启用,因为 UI 显示第 2 行中存在 iframe,因此您会收到超时异常
    • 我想我现在明白了,那么我怎样才能获得 iframe 的动态 xpath。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2017-03-21
    • 2013-05-02
    • 2012-09-16
    • 2019-08-17
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多