我正在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&header=EnterpriseChangeMgt.Label.ECMChangeCOsCAsLeagcySummaryPageHeading&emxSuiteDirectory=enterprisechangemgt&treeLabel=91852699+001&suiteKey=EnterpriseChangeMgt&StringResourceFileId=emxEnterpriseChangeMgtStringResource&SuiteDirectory=enterprisechangemgt&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&PrinterFriendly=true&SuiteDirectory=enterprisechangemgt&treeLabel=91852699+001&parentOID=20336.41905.42762.46685&emxSuiteDirectory=enterprisechangemgt&HelpMarker=emxhelpchangeordersection&program=enoECMChangeUtil%3AgetConnectedChanges&functionality=isChangeOrderTab&portalMode=true&StringResourceFileId=emxEnterpriseChangeMgtStringResource&toolbar=ECMChangeOrderManagementToolbar&sortDirection=ascending&selection=multiple&portalCmdName=ECMCOs&jsTreeID=null&suiteKey=EnterpriseChangeMgt&header=EnterpriseChangeMgt.Label.ECMChangeCOsCAsLeagcySummaryPageHeading&portal=ECMEnterpriseChangeMgtPortal&table=ECMChangeSummary&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
:
答案 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'))