点击iframe中的链接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点击iframe中的链接相关的知识,希望对你有一定的参考价值。

我在VBA代码中搜索单击iframe中链接的方法

我还做了什么:

Set IEApp = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
IEApp.Visible = True
IEApp.navigate "https://meineseite.../LoginForm.jsp" 'anpassen
Start = Now
Do: Loop Until IEApp.Busy = False
Do: Loop Until IEApp.Busy = False
Do: Loop Until IEApp.Document.ReadyState = "complete"
Do
'On Error Resume Next
If DateDiff("s", Start, Now) > 0 Then Exit Do
Loop Until IEApp.ReadyState <> 4
Set IEDocument = IEApp.Document
IEDocument.all.USER.Value = "Benutzer"
IEDocument.all.PASS.Value = "Passwort"
IEDocument.getElementsByName("LoginForm")(0).submit

工作完美。所以现在有一个字段可以输入名称,然后它会打开一个包含2个或更多链接的框架。所以我必须搜索右边。它完成了...但我怎么能点击其中一个链接?

我试过这些:

Call IEDocument.frames(1).execScript("choose('Objektnummer, Referenznummer')", "javascript")
    IEDocument.parentWindow.frames.Navigate "choose('Objektnummer, Referenznummer')"
    IEDocument.parentWindow.Links(2).Click

也许我写得像那样:

IE.Document.getElementById("?").contentDocument.getElementById("?").Click

但我把什么投入“?” ?

所以这里是iframe代码:

DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
  <script type="text/javascript">
    function choose(wert) {
      top.document.getElementById("links").value=wert;
      parent.absenden("LINKS");
      top.document.getElementById("IFRAME").style.visibility = "hidden";
    }
    function close(){
      top.document.getElementById("IFRAME").style.visibility = "hidden";
    }
  </script>
<body bgcolor="white">
  <span style="font-family:sans-serif; font-size:12px;">
    <table style="font-size:12px;font-weight:none;" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2" align="right">
          <a href="JavaScript:close();" style="text-decoration:none;color:#000000;font-weight:bolder;">X&nbsp;&nbsp;</a>
        </td>
      </tr>
      <tr> <td colspan='2' nowrap="nowrap"> <a href="javascript:choose('nichtgewaehlt')">Bitte wählen</a> </td> </tr>
<tr> <td colspan='2' nowrap="nowrap"> <a href="javascript:choose('Objektnummer, Referenznummer')">Objektname</a> </td> </tr>
<tr> <td colspan='2' nowrap="nowrap"> <a href="javascript:choose('Objektnummer2, Referenznummer2')">Objektname2</a> </td> </tr>
</table>
  </span>
</body>
</html>

和完整的srccode

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Deutsches Format dd.mm.yyyy -->
<script language="JavaScript" src="calendar_de.js"></script><!-- Datum mit Jahresauswahlmoeglichkeit -->
<!-- Englisches Format mm/dd/yyyy -->
<script language="JavaScript" src="calendar_en.js"></script><!-- Datum mit Jahresauswahlmoeglichkeit -->
<LINK REL="STYLESHEET" media="screen" TYPE="text/css" HREF="__default.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Auswahl</title>

</head>
<script type="text/javascript">
function absenden(feldname)
{
document.CheckForm.FKT.value = feldname;
if ("SUBMITREP" == feldname) {
document.CheckForm.SUBTYP.value="SUBANDREP";
}
document.CheckForm.submit();
document.CheckForm.ANZB.value="";
}

function enableBtn(wert) {
if (wert > 0) {
document.CheckForm.btn_submit_repair.disabled=false;
}
else {
document.CheckForm.btn_submit_repair.disabled=true;
}
}

function Auswahl_link(){
var vLink = document.getElementById("link");
var vNameNr = document.getElementById("Name");
var vMin = 0;

// Link in Grossbuchstaben umwandeln
vLink.value = vLink.value.toUpperCase();
// bei 'ESC' Popup schließen
if(window.event.keyCode == 27) {
top.document.getElementById("IFRAME").style.visibility = "hidden";
return;
}


// Workaround für fehlerhaftes Encoding von Umlauten beim ersten Aufruf
if (vLink.value.length > vMin-1) {
window.IFRAME.location.href = "PopupLink.jsp?LINK="+vLink.value+
"&LANG=de"+
"&Name=G"+
"&Ort="+"B".replace("%","%25")+
"&Gliederung="+document.HauptForm.Gliederung.value.replace("%","%25")+
"&Name="+vNameNr.value.replace("%","%25")+
"&SORT1=1"+
"&Mandat=Bereich";
}
if (vLink.value.length > vMin) {
var Element = document.getElementById("link");
var x=0, y=0;
var pos=0;
while (Element) {
x = x + Element.offsetLeft;
y = y + Element.offsetTop;
Element = Element.offsetParent;
}
document.getElementById("IFRAME_DIV").style.top = y + 25;
document.getElementById("IFRAME_DIV").style.left = x;
document.getElementById("IFRAME").width = 700;
document.getElementById("IFRAME").height = 200;
document.getElementById("IFRAME").style.backgroundColor = 'white';
document.getElementById("IFRAME").style.visibility = "visible";
} else {
document.getElementById("IFRAME").style.visibility = "hidden";
}
}

function Auswahl_NameNr(){
var vNameNr = document.getElementById("Name");
var vMin = 0;

// Link in Grossbuchstaben umwandeln
vNameNr.value = vNameNr.value.toUpperCase();
// bei 'ESC' Popup schließen
if(window.event.keyCode == 27) {
top.document.getElementById("IFRAME").style.visibility = "hidden";
return;
}


if (vNameNr.value.length > vMin-1) {
window.IFRAME.location.href = "PopupNameNr.jsp?NAMEANR="+vNAMENr.value.replace("%","%25")+
"&LANG=de"+
"&Name=G"+
"&Ort="+"B".replace("%","%25")+
"&Gliederung="+"%".replace("%","%25")+
"&SORT1=1"+
"&Mandat=Bereich";
}
if (vNameNr.value.length > vMin) {
var Element = document.getElementById("NAME");
var x=0, y=0;
var pos=0;
while (Element) {
x = x + Element.offsetLeft;
y = y + Element.offsetTop;
Element = Element.offsetParent;
}
document.getElementById("IFRAME_DIV").style.top = y + 25;
document.getElementById("IFRAME_DIV").style.left = x;
document.getElementById("IFRAME").width = 300;
document.getElementById("IFRAME").height = 200;
document.getElementById("IFRAME").style.backgroundColor = 'white';

document.getElementById("IFRAME").style.visibility = "visible";
} else {
document.getElementById("IFRAME").style.visibility = "hidden";
}
}

function Auswahl(){
if (document.getElementById("IFRAME").height == 125 && document.getElementById("IFRAME").style.visibility == "visible"){
document.getElementById("IFRAME").style.visibility = "hidden";
}
else {
window.IFRAME.location.href = "PopupAuswahl.jsp?MANDAT=Bereich"+
"&ORT="+document.HauptForm.ORT.options[document.HauptForm.ORT.selectedIndex].text.replace("%","%25")+
"&GLIEDERUNG="+document.HAUPTForm.GLIEDERUNG.value.replace("%","%25");

var Element = document.HauptForm.GLIEDERUNG;
var x=0, y=0;
while (Element) {
x = x + Element.offsetLeft;
y = y + Element.offsetTop;
Element = Element.offsetParent;
}
document.getElementById("IFRAME_DIV").style.top = y + 16;
document.getElementById("IFRAME_DIV").style.left = x;
document.getElementById("IFRAME").width = 160;
document.getElementById("IFRAME").height = 125;
document.getElementById("IFRAME").style.backgroundColor = 'white';
document.getElementById("IFRAME").style.visibility = "visible";
}
}

function Auswahl_BELinkNr(){
var vBELinkNr = document.getElementById("belinknnr");
var vMin = 0;

// Link in Grossbuchstaben umwandeln
vBELinkNr.value = vBELinkNr.value.toUpperCase();
// bei 'ESC' Popup schließen
if(window.event.keyCode == 27) {
top.document.getElementById("IFRAME").style.visibility = "hidden";
return;
}


if (vBELinkNr.value.length > vMin) {
window.IFRAME.location.href = "PopupBELinknNr.jsp?BELINKNR="+vBELinkNr.value.replace("%","%25")+
"&BTGRP="+"%".replace("%","%25")+
"&BTKLA="+"%".replace("%","%25")+
"&MANDAT=Bereich";
var Element = document.getElementById("belinknr");
var x=0, y=0;
var pos=0;
while (Element) {
x = x + Element.offsetLeft;
y = y + Element.offsetTop;
Element = Element.offsetParent;
}
document.getElementById("IFRAME_DIV").style.top = y + 25;
document.getElementById("IFRAME_DIV").style.left = x ;
document.getElementById("IFRAME").width = 700;
document.getElementById("IFRAME").height = 200;
document.getElementById("IFRAME").style.backgroundColor = 'white';

document.getElementById("IFRAME").style.visibility = "visible";
} else {
document.getElementById("IFRAME").style.visibility = "hidden";
}
}
</script>

<body onLoad="javascript:document.CheckForm.PDAT.focus();">

<div id="IFRAME_DIV" style="position:absolute;z-index:2;visibility:hidden;">
<iframe src="JavaScript:void(0);" id="IFRAME" name="IFRAME" width="650" height="200" marginheight="0" marginwidth="0" align="middle" scrolling="yes">
</iframe>
</div>

<!-- ===== top navigation ===== -->
<div style="position:absolute;top:0;left:0;">
<TABLE border="0" height="100%" width="100%" cellspacing="0" cellpadding="0">
<TR ALIGN="center" NOWRAP bgcolor="#336699" height="1%">
<TD align="right" rowspan="3" width="1%">
<A HREF="Prozess.COMMON.SHOW" onMouseOver="document.keyvisual.src='img/key-visual_light.gif';" onMouseOut="document.keyvisual.src='img/key-visual.gif';">
<IMG SRC="img/key-visual.gif" name="keyvisual" border="0" width="144" height="83">
</A>
</TD>
<TD width="1%"></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Sprache</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Ort</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Name</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Gerät</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Prozess</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>Sortierung</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>&nbsp</nobr></TD>
<TD ALIGN="left" style="color:#FFFFFF;"><nobr>&nbsp</nobr></TD>
<TD width="1%"></TD>
</TR>
<TR ALIGN="center" NOWRAP bgcolor="#336699" height="1%">
<form name="ArbeitsplatzForm" action=/delta-beleg/HauptContro.jsp method=post>
<TD></TD>
<TD align="left" valign="top" NOWRAP>
<select name="LANG" size="1">
<option selected value="de" >Deutsch</option>
<option value="en" >Englisch</option>
</select>
</TD>
<TD align="left" valign="top" NOWRAP>
<SELECT name="BEREICH" size="1">
<OPTION>%</OPTION>
<OPTION>A</OPTION>
<OPTION selected>B</OPTION>
<OPTION>C</OPTION>
<OPTION>nn</OPTION>
</SELECT>
&nbsp;</TD>
<TD align="left" valign="top" NOWRAP>
<span style="position:relative;top:-6px;left:0px;"><INPUT readonly onClick="Auswahl_Gliederung();" name="GLIEDERUNG" id="FAGRU" type="text" value="%" size="21"/><IMG onClick="Auswahl_Gliederung();" src="img/DropDown.gif" height="22" width="18" style="position:relative;top:5px;left:-1px;"/>&nbsp;</span> </TD>
<TD align="left" valign="top" NOWRAP>
<INPUT type="text" name="NAME" id="NAME" onKeyUp="Auswahl_NameNr();" value="%" size="21"/> </TD>
<TD align="left" valign="top" NOWRAP>
<select name="NAME" size="1">
<option value="fp" >Prozess1</option>
<option value="fr" >Prozess2</option>
<option value="bp" >Prozess3</option>
<option value="br" >Prozess4</option>
<option selected value="gp" >Prozess5</option>
<option value="gr" >Prozess6</option>
<option value="rf" >Prozess7</option>
</select>
</TD>
<TD align="left" valign="top" NOWRAP>
<select name="SORT1" size="1">
<option selected value="1" >NAME, Nr, Index</option>
<option value="2" >Nr, Index, Name</option>
</select>
</TD>
<TD align="left" valign="top" NOWRAP>
&nbsp
</TD>
<TD align="left" valign="top" NOWRAP>
<input type="submit" value="Absenden">
</TD>
<TD ALIGN="left" width="50"></TD>
</FORM>
</TR>
<TR ALIGN="center" NOWRAP height="1%" bgcolor="#99CCFF">
<TD colspan="6" align="right" nowrap="nowrap"></TD>
<TD colspan="2" align="left" nowrap="nowrap">Benutzer</TD>
<TD align="left" nowrap="nowrap">
<nobr>&nbsp;</nobr><A HREF="LoginForm.jsp" class="navigation">logout</A>
</TD>
<TD ALIGN="left" width="50"></TD>
</TR>
</TABLE>
</div>
<!-- ===== END - top navigation ===== -->
<!-- ===== left navigation ===== -->
<div style="position:absolute;top:83;left:0;z-index:0;">
<TABLE width="144" height="445" bgcolor="#99CCFF">
<TR>
<TD></TD>
</TR>
</TABLE>
</div>
<div style="position:absolute;top:83;left:0;">
<TABLE border="0" width="144" height="41" bgcolor="#336699" cellspacing="0" cellpadding="0">
<TR bgcolor="#336699">
<TD>&nbsp;</TD>
<TD align="LEFT" style="color:#FFFFFF;font-weight:bold;">DELTA-<br> FER</TD>
</TR>
</TABLE>
</div>
<!-- ===== inner page ===== -->
<div style="position:absolute;top:86;left:144;">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="300" align=left valign=bottom>
<img src="img/chip_01.jpg" width=300 height=24 border=0>
</td>
<td align="left" valign="bottom" style="FONT-FAMILY:Arial;FONT-SIZE:18px;font-weight:bold;COLOR:#000000;">Geräte-Prüfung</td>
<td align="right" valign="bottom"><a href="./help.htm">
<img src="img/icohelp1.gif" width=24 height=20 border=0 alt="Hilfe"></a>
</td>
</tr>
<tr>
<td colspan=3 align=left bgcolor=#0000ff>
<img src="img/1x1.gif" width=1 height=1 border=0>
</td>
</tr>
</table>
<table ALIGN="CENTER" width="100%">
<TR>
<TD WIDTH="1%" colspan="2"></TD>
</TR>
</TABLE>
<!-- ===== START - Anwendungsbereich ===== -->
<form name="CheckForm" action="/delta-beleg/CheckContro.jsp" method=post width="500" autocomplete="off">
<center>
<table cellpadding=4 cellspacing=2 border=0>

<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>Pruefdatum</b>
<br>
<input type="text" name="PDAT" size=10 value="" maxlength=10><a href="javascript:cal1.popup();"><img src="img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a>
<script language="JavaScript">
function setHeute(){
var jetzt = new Date();
var Tag = jetzt.getDate()<10?"0"+jetzt.getDate():jetzt.getDate();
var Monat = jetzt.getMonth()+1<10?"0"+(jetzt.getMonth()+1):(jetzt.getMonth()+1);
var Jahr = jetzt.getFullYear();

var txt = Tag+"."+Monat+"."+Jahr;

document.getElementsByName("PDAT")[0].value = txt;
}
</script>
&nbsp;&nbsp;<a href="JavaScript:setHeute();" style="font-size:10px;text-decoration:none;color:#000000;cursor:hand;">[heute]</a>
</td>
</tr>

<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>Gerät / Gerät St</b>
<br>
<input type="text" name="LINK" id="link" size="70" value="" onKeyUp="Auswahl_Link();">
</td>
</tr>



<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>Plan</b>
<br>
<SELECT name="PLALT" size="1" null>
<OPTION selected value="-" >-, -</OPTION>
</SELECT>
</td>
</tr>
<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>Anzahl geprueft</b>
<br>
<input type="text" name="ANZG" size=10 value="" maxlength=10></td>
</tr>

<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>Anzahl beanstandet</b>
<br>
<input type="text" name="ANZB" size=10 value="" maxlength=10 onkeyup="enableBtn(this.value)">
</td>
</tr>

<!--
<tr bgcolor="#c8d8f8">
<td valign=top colspan=2>
<b>%= Translator.getTranslation(Constants.CHECKFORM_ANZ_REPARIERT, lang) %></b>
<br>
<input type="text" name="%=Constants.PARA_ANZ_REPARIERT %>" size=10 value="" maxlength=10>
</td>
</tr>
-->



<tr bgcolor="#c8d8f8">
<td align=center colspan=2>
<input type="button" value="Speichern" onClick="absenden('SUBMIT')">
<input type="button" name="btn_submit_repair" value="Speichern" onClick="absenden('SUBMITREP')">
<input type="button" value="Zurücksetzen" onClick="absenden('CLEAR')">

</td>
</tr>

</table>
<input type="hidden" name="FKT" value="">
<input type="hidden" name="SUBTYP" value="">
</center>
</form>

<!-- ===== END - Anwendungsbereich ===== -->
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" align=left bgcolor=#0000ff>
<img src="img/1x1.gif" width=1 height=1 border=0>
</td>
</tr>
<tr>
<td align="left" valign="top" width="1%">
<img src="img/chip_01b.jpg" width="300" height="23" border="0">
</td>
<td align="left" valign="bottom">&nbsp;</td>
<td align="right" valign="bottom">
<SCRIPT LANGUAGE='JavaScript'> jetzt = new Date(); Std = jetzt.getHours(); Min = jetzt.getMinutes(); Tag = jetzt.getDate(); Jahr = jetzt.getYear();
if(Jahr < 100) Jahr+=1900; Jahresmonat = jetzt.getMonth(); var Monat = new Array("Jan","Feb","M&auml;r","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez");
var StdAusgabe = ((Std < 10) ? "0" + Std : Std); var MinAusgabe = ((Min < 10) ? "0" + Min : Min);
document.write("<span style='color:#000000;font-size:10px;font-family:Arial,sans-serif;'>Stand: " + Tag+ "." + Monat[Jahresmonat] + "." + Jahr + " " + StdAusgabe + ":" + MinAusgabe + " Uhr" + "&nbsp;</span><br/>");</SCRIPT>
<span style="FONT-FAMILY:Arial;FONT-SIZE:12px;font-weight:900;COLOR:#000055;">---&nbsp;</span>
</td>
</tr>
</table>
</div>
<script language="JavaScript">
<!-- // create calendar object(s) just after form tag closed
// specify form element as the only parameter (document.forms['formname'].elements['inputname']);
// note: you can have as many calendar objects as you need for your application
var cal1 = new calendar_de(document.forms['CheckForm'].elements['PDAT']);
cal1.year_scroll = true;
cal1.time_comp = false;
//-->
</script>

</body>
</html>

非常感谢你

答案

尝试使用CSS选择器来定位iframe中的元素。

如果你认为这是一个时间问题,你可以

1)循环,而目标元素没有任何东西,即没有设置;超时以避免无限循环的可能性

Dim t As Date, ele As Object, waitTime As Long
t = Timer
waitTime = 10 '<== secs
Do
    DoEvents
    On Error Resume Next
    Set ele = IEDocument.frames("IFRAME").document.querySelector("a[href=javascript:choose('Objektnummer, Referenznummer')]")
    On Error GoTo 0
    If Timer - t = waitTime Then Exit Do     '<==To avoid infinite loop
Loop While ele Is Nothing
Call IEDocument.iframe.execScript("Choose('Objektnummer,Referenznummer')", "JavaScript")

2)设置明确的等待

Application.Wait Now + TimeSerial(0,0,5) '<== adjust 5 which is number of seconds
Call IEDocument.iframe.execScript("Choose('Objektnummer,Referenznummer')", "JavaScript")

以上是关于点击iframe中的链接的主要内容,如果未能解决你的问题,请参考以下文章

Jquery - 拦截在 iframe 内点击的链接

Layuilayui中iframe子页面中的链接点击后在父页面中动态添加tab选项

Layuilayui中iframe子页面中的链接点击后在父页面中动态添加tab选项

网页中如何实现点击iframe中的一个链接,使整个页面(不是只有iframe)跳转到新的页面

微信小程序代码片段

点击链接 javascript 播放 iframe 视频