【问题标题】:How to handle dynamic iFrame ID in Python (Selenium)如何在 Python (Selenium) 中处理动态 iFrame ID
【发布时间】:2021-06-29 13:18:49
【问题描述】:

我有一个想要切换到的 iFrame,但 iFrame 每次都会生成一个随机 ID。如何切换到此 iFrame。据我所知,资源保持一致。

更新:点击下载有效,但点击关闭无效。

<iframe id="e674" src="/resource.ashx/636625958320000000/du/webviewer/html5/Reader.html?setLng=en#d=%2Fdocumentviewer.ashx%2FDownloadDocument%3FstateId%3Dv3x4o%26cacheInfoKey%3D1arhoq9%2F1arhoq9-1o5aabb.json&amp;auto_load=false&amp;languageUrl=%2Fresource.ashx%2F636625958320000000%2Fdu%2Flocalization.json%3Fdisplay%3D__lng__&amp;custom=%7B%22permissions%22%3A65535%2C%22showToolbar%22%3Atrue%2C%22showSidePane%22%3Atrue%2C%22singlePageRotation%22%3Afalse%2C%22downloadUrl%22%3A%22%2Fdocumentviewer.ashx%2FDownloadSource%3FstateId%3Dv3x4o%26cacheInfoKey%3D1arhoq9%2F1arhoq9-1o5aabb.json%22%2C%22downloadAsPdfUrl%22%3A%22%2Fdocumentviewer.ashx%2FDownloadAsPdf%3FstateId%3Dv3x4o%26cacheInfoKey%3D1arhoq9%2F1arhoq9-1o5aabb.json%22%7D&amp;filepicker=0&amp;preloadWorker=1&amp;pdfnet=0&amp;pageHistory=1&amp;subzero=1" frameborder="0" width="100%" height="100%" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>


driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@name='rwDocumentviewer']"))

                time.sleep(3)

                driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'webviewer/html5/Reader.html')]"))

                #Click on download
                downloadButton = driver.find_element_by_xpath('//*[@id="downloadButton"]')
                downloadButton.click()

                time.sleep(3)

                #Click on close
                closeButton = driver.find_element_by_xpath('//*[@id="btnZorgAnnuleren"]')
                closeButton.click()            

                driver.switch_to.default_content()

【问题讨论】:

  • 我投票结束这个问题,因为这里被问到stackoverflow.com/questions/68176797/…
  • 这是一个不同的问题,因为我在这里讨论如何处理动态 iFrame (ID)。
  • 我也对该帖子发表了评论。如果没有解决,请告诉我们出了什么问题以及为什么需要这个问题。如果它看起来很真实,我自己肯定也会看看这个问题
  • 对不起,我对 SO 有点陌生。当你有一个新问题时,我以为你应该开始一个新帖子。抱歉!
  • 我明白,但是当有人回答你的问题时,你可以评论它是否有效,我认为这也和这个问题一样。

标签: python selenium


【解决方案1】:

看看这是否有效:

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@name='rwDocumentviewer']"))

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'webviewer/html5/Reader.html')]"))

【讨论】:

  • 我得到这个:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//iframe[contains(@src,'du/webviewer /html5/Reader.html')]"} 当我这样做时。
  • 编辑了我的代码。试试看它是否有效
  • 不幸的是,我收到了同样的信息。非常感谢您的帮助!
  • 它是否在另一个 iframe 中?可以查一下吗?
  • ctrl+f 时有 3 个:&lt;iframe name="rwDocumentviewer" src="DocumentViewer.aspx?nr=7147" frameborder="0" style="width: 100%; height: 100%; border: 0px;"&gt;&lt;/iframe&gt;,和,&lt;iframe style="display: none;"&gt;&lt;/iframe&gt;。第二个是帖子里的那个。
猜你喜欢
  • 2014-06-09
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 2018-10-30
  • 2012-04-14
  • 1970-01-01
相关资源
最近更新 更多