Python,Selenium:无法找到第四个嵌套的iFrame

时间:2018-07-01 22:30:39

标签: python selenium-webdriver

我正在Python中使用Selenium来自动执行网页上的某些任务。网页上有一些嵌套的iFrame,我需要深入到第四个嵌套的iFrame中才能单击一个按钮。前三个嵌套的iFrame可以快速切换到第四个iFrame,但是即使设置了120s的等待时间,我也总是会遇到超时错误。以下是Python代码和HTML。 iframe name =“ portalDisplay”>是第三个嵌套的iframe,iframe name =“ ECMCOs”是我要切换到的那个,然后单击其中的一个按钮。谢谢!

browser.switch_to_default_content()
WebDriverWait(browser, 60).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@id="content"]'))) 
WebDriverWait(browser, 60).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//div[@id="unique1"]/iframe[1]')))
WebDriverWait(browser, 60).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//div[@id="divPageBody"]/iframe[1]')))
WebDriverWait(browser, 120).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'.//iframe[@name="ECMCOs"]')))

<iframe name="portalDisplay" src="emxPortalDisplay.jsp?portal=ECMEnterpriseChangeMgtPortal&amp;header=EnterpriseChangeMgt.Label.ECMChangeCOsCAsLeagcySummaryPageHeading&amp;emxSuiteDirectory=enterprisechangemgt&amp;treeLabel=91852699+001&amp;suiteKey=EnterpriseChangeMgt&amp;StringResourceFileId=emxEnterpriseChangeMgtStringResource&amp;SuiteDirectory=enterprisechangemgt&amp;objectId=20336.41905.42762.46685" width="100%" height="100%" frameborder="0" border="0" cd_frame_id_="ca6315bf2f2783f6030dd845f480d61d">
#document
<html>
<head>..<head>
<body>
<!-- //XSSOK -->
<script language="javascript" src="/enovia/common/scripts/emxUICore.js"></script>
<!-- //XSSOK -->
<script language="javascript" src="/enovia/common/scripts/emxUIModal.js"></script>
<script language="Javascript"> 
</script>
<script language="JavaScript">   var DisplayErrorMsg = "";
 </script>

<div class="pv-container" id="divPowerView" data-portalname="ECMEnterpriseChangeMgtPortal" style="overflow-y: hidden;">
  <div class="pv-row" id="divPvRow-1" minsize="240" style="height: 709px;">
    <div class="pv-channel complex" id="divPvChannel-1-1" style="width: 100%; 
     float: right; height: 100%;">
       <div class="pv-channel-tabs" id="pvChannelTabs">
         <table cellpadding="0" cellspacing="0" border="0"> 
          ....
          </div>
        <div class="pv-channel-content" style="">
           <div class="tab-header" style="display: none;"></div>
           <iframe name="ECMCOs" src="../common/emxIndentedTable.jsp? 
 sortColumnName=Name&amp;PrinterFriendly=true&amp;SuiteDirectory=enterprisechangemgt&amp;treeLabel=91852699+001&amp;parentOID=20336.41905.42762.46685&amp;emxSuiteDirectory=enterprisechangemgt&amp;HelpMarker=emxhelpchangeordersection&amp;program=enoECMChangeUtil%3AgetConnectedChanges&amp;functionality=isChangeOrderTab&amp;portalMode=true&amp;StringResourceFileId=emxEnterpriseChangeMgtStringResource&amp;toolbar=ECMChangeOrderManagementToolbar&amp;sortDirection=ascending&amp;selection=multiple&amp;portalCmdName=ECMCOs&amp;jsTreeID=null&amp;suiteKey=EnterpriseChangeMgt&amp;header=EnterpriseChangeMgt.Label.ECMChangeCOsCAsLeagcySummaryPageHeading&amp;portal=ECMEnterpriseChangeMgtPortal&amp;table=ECMChangeSummary&amp;objectId=20336.41905.42762.46685" frameborder="0" width="100%"></iframe></div>
<div class="pv-channel-content" style="display: none;">
<div class="tab-header" style="display: none;"></div><iframe name="ECMCRs" src="" frameborder="0" width="100%"></iframe></div><div class="pv-channel-content" style="display: none;"><div class="tab-header" style="display: none;"></div><iframe name="ECMChangeLegacy" src="" frameborder="0" width="100%"></iframe></div></div></div></div></body></html>

TML代码段enter code here

2 个答案:

答案 0 :(得分:0)

我认为您已经很接近了。从以下位置更改定位器策略

(By.XPATH,'.//iframe[@name="ECMCOs"]')
       ^ remove the dot

收件人:

(By.XPATH,"//iframe[@name='ECMCOs'][contains(@src,'/common/emxIndentedTable.jsp')]")

答案 1 :(得分:0)

我终于通过使用以下代码设法适应它:

browser.switch_to_default_content()
iframe1 = WebDriverWait(browser, 
120).until(EC.presence_of_element_located((By.XPATH,'//iframe[@id="content"]')))
browser.switch_to.frame(iframe1.get_attribute('id'))
iframe2 = WebDriverWait(browser,120).until 
(EC.presence_of_element_located((By.XPATH,'//div[@id="unique1"]/iframe[1]')))
browser.switch_to.frame(iframe2.get_attribute('name'))
iframe3 = WebDriverWait(browser,120).until 
(EC.presence_of_element_located((By.XPATH,'//div[@id="divPageBody"]/iframe[1]')))
browser.switch_to.frame(iframe3.get_attribute('name'))
time.sleep(20)
iframe4 = WebDriverWait(browser, 120).until 
(EC.presence_of_element_located((By.XPATH,'//iframe[@name="ECMCOs"]')))
browser.switch_to.frame(iframe4.get_attribute('name'))
  1. 使用框架名称进行切换(根据建议here
  2. 棘手的是使用time.sleep()而不是browser.implicitly_wait()。我以前尝试过browser.implicitly_wait(),但是即使我已经设置了browser.implicitly_wait(70),仍然无法正常工作,并且我尝试从当前音符中打印出第一个iframe(.// iframe [0])我收到了iframe @ id =“ content”,感觉好像我没有切换框架。我的猜测是使用隐式等待,当WebDriver轮询DOM尝试查找元素时,WebDriver会退出iframe。在使用睡眠模式时,WebDriver会保留在iframe中